web
You’re offline. This is a read only version of the page.
close
Skip to main content
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 644

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)
  • Suggested answer
    D365FO Avatar Profile Picture
    644 on at
    RE: How to restrict a batch job to run within a specific timeline

    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.

  • Anton Venter Profile Picture
    20,281 Super User 2025 Season 2 on at
    RE: How to restrict a batch job to run within a specific timeline

    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.

  • D365FO Avatar Profile Picture
    644 on at
    RE: How to restrict a batch job to run within a specific timeline

    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?

  • Suggested answer
    Anton Venter Profile Picture
    20,281 Super User 2025 Season 2 on at
    RE: How to restrict a batch job to run within a specific timeline

    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));

  • Martin Dráb Profile Picture
    236,258 Most Valuable Professional on at
    RE: How to restrict a batch job to run within a specific timeline

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

  • D365FO Avatar Profile Picture
    644 on at
    RE: How to restrict a batch job to run within a specific timeline

    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.

  • Verified answer
    Martin Dráb Profile Picture
    236,258 Most Valuable Professional on at
    RE: How to restrict a batch job to run within a specific timeline

    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.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

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

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 2,257

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 611 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans