web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to restrict a batch job to run within a specific timeline

(0) ShareShare
ReportReport
Posted on by 668

Hi Folks,

I have been working with a batch job which is running from weeks. I want to restrict the execution time so that it will end after executing some no. of records at a time and than continue again.

Please help to resolve this issue.

Thanks in Advance!!!

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    It's not clear to me what you need from us.

    Simply count processed records in your batch job and end when you reach the limit.

  • D365FO Avatar Profile Picture
    668 on at

    Yes, as It has millions of records to process.

    I was thinking to add a parameter of "Number of hours to execute the job" in the batch job. And to convert it into seconds and when it reaches  more than no. of hours mentioned in the parameter it will end.

    So I want to know can we do through this? And is there is any function in X++ which returns seconds from current date and time.

  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    First of all, decide what you actually want. The number of records, as you asked before, or time, as you're asking now.

  • Suggested answer
    Anton Venter Profile Picture
    20,345 Super User 2025 Season 2 on at

    You can use System.Diagnostics.Stopwatch to figure out how long the batch process has been running and stop it when it has exceeded the max duration.

    Here is a simple example of how to use it.

    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
    sw.Start();
    
    //do something
    
    info(strFmt("Done. Duration %1s.", sw.ElapsedMilliseconds/1000));

  • D365FO Avatar Profile Picture
    668 on at

    Hi Anton,

    Yes, I needed something like this only. Thanks for your response. Could you please let me know can I debugg it without in running it in batch process?

    and how can I stop the batch job through after specific seconds?

  • Anton Venter Profile Picture
    20,345 Super User 2025 Season 2 on at

    Or even simpler, you can just use TimeNow(). I cannot tell you without knowing more information about the batch job. How to stop it depends on the batch job. Need more info about it.

  • Suggested answer
    D365FO Avatar Profile Picture
    668 on at

    Hi Martin,

    Thanks, I ended with this solution only where I added a new parameter in the batch job which give the number of hours a batch job should be run.

    Simultaneously counting the number of record and after reaching a specific time under that time interval, I m ending the batch job.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 611 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 529 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 285 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans