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, ...
Suggested Answer

Update the batch job status of running batch through Code in D365 F&O

(0) ShareShare
ReportReport
Posted on by 758

Hi Everyone,

I am scheduling a batch job through Code and setting the recurrence of once in a day.

The batch job has logic written of updating 2 fields of a standard table conditionally.

That means, it will look for some conditions and if that conditions have been met, then only it will update the fields.

And once it updates the field of table, I want to stop the further recurrence of batch job and have to end the job.

Can anyone please confirm, how this can be achieved?

Thank you in advance.

I have the same question (0)
  • VRD Profile Picture
    on at

    Are you saying that the batch job should not run ever after you update those 2 fields?

    Assuming this is you need, I am giving following solution,

    You can create a 3rd field and set value of it to 'True' and  check if that is False in the first line of the job and only then proceed for further check.

    So that way, when 1st time the job runs, it will have this field to 'False' and then it will go and update your 2 fields and then also marks this 3rd field to 'True'.

    When 2nd time the job runs, it will find this value as 'True' and won't proceed further.

    Now it is you who will decide that where to place this 3rd field.

  • Suggested answer
    ergun sahin Profile Picture
    8,826 Moderator on at

    community.dynamics.com/.../batch-job-schedule-x

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

    I would remove the recurrence setup when no more runs of this batch are needed.

  • Suggested answer
    ergun sahin Profile Picture
    8,826 Moderator on at

    // No end date
    //sysRecurrenceData = SysRecurrence::setRecurrenceNoEnd(sysRecurrenceData);
    // Finish after X times
    sysRecurrenceData = SysRecurrence::setRecurrenceEndAfter(sysRecurrenceData, 1);

    I'm not sure if it works with 0, the fact is I haven't tested the build but logically it should stop when you can update it with code.

    Maybe it will work to give the default value of sysRecurrenceData. (need to test)

  • Suggested answer
    Sheikh Sohail Profile Picture
    6,125 on at

    Hi

    Please set the end date once your logic is executed successfully.

    sysRecurrenceData = SysRecurrence::defaultRecurrence();

    SysRecurrence::setRecurrenceStartDateTime(sysRecurrenceData, DateTimeUtil::addSeconds(DateTimeUtil::utcNow(), 20));

    SysRecurrence::setRecurrenceNoEnd(sysRecurrenceData);

  • Rhushikesh R Profile Picture
    758 on at

    Thanks Sohail, Sahin, Martin,

    But , can you please confirm, where should I put this logic? I have used SysOperationFramework.

    Should I write it in Service class or what? If I use SysRecurrence, how should I pass the current batch to it?

  • Suggested answer
    Rhushikesh R Profile Picture
    758 on at

    Hi everyone,

    I implemented my code as below in my Service class and its working as expected now. Whenever I am updating fields, I am modifying my recurrence to 1 and batch job is getting Ended.

    ttsbegin;
    BatchHeader batchHeader = this.getCurrentBatchHeader();
    SysRecurrenceData  recurrenceData = batchHeader.parmRecurrenceData();
    recurrenceData = SysRecurrence::setRecurrenceEndAfter(recurrenceData, 1);
    batchHeader.parmRecurrenceData(recurrenceData);
    batchHeader.save();
    ttscommit;

    Thanks everyone for suggestions.

  • Suggested answer
    Sheikh Sohail Profile Picture
    6,125 on at

    Please mark the answer as verified, it will help others in the future to pick the answers.

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 503 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 278 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans