Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Answered

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

(0) ShareShare
ReportReport
Posted on by 457

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!!!

  • Suggested answer
    D365FO Avatar Profile Picture
    D365FO Avatar 457 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
    Anton Venter 19,493 Super User 2025 Season 1 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
    D365FO Avatar 457 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
    Anton Venter 19,493 Super User 2025 Season 1 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
    Martin Dráb 230,900 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
    D365FO Avatar 457 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
    Martin Dráb 230,900 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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,074 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,900 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans