Thursday, November 7, 2019

AX 2012 R3 - Retail - Batch job dependencies or sequencially running

P-jobs or pull jobs for the retail module in AX 2012 R3 are the jobs that are used to upload daily retail sales transactions from outlets to HQ. For these, the packets are composed in a working folder, and that folder has further subfolders against each retail store or outlet. The organized or planned batch job running will avoid any conflict or deadlock occurring on the same tables e.g. RetailTransactionTable, or RetailTransactionSalesTrans, or any other retail table where these p-jobs will insert the new records. By organized or planned means, we can actually use some tweaking, merge separate P-jobs in batch into a single job and various tasks, where each task will wait for the completion of prior tasks, so in this manner, a queue will be created where no task will block or result into a deadlock for another task to fail.

There are three tables involve in this setup, namely Batch, BatchJob, and BatchJobConstraints. BatchJob table contains the header information of the batch job, whereas the Batch table contains individual tasks within a batch job. The BatchJobConstraints contain dependencies or conditions of tasks upon one another. Also, worthy to note here is the fact that a BatchJob record which is a parent can contain multiple tasks or child records in the Batch table. In our case, we want to merge child records belonging to the same number of parents, to belong to a single parent instead. In this way, we'll be able to impose conditions on individual tasks to depend upon each other when they will belong to a single parent record. In this way, they will run in the queue rather than at the same time to block or create deadlock which we want to avoid for the jobs to run smoothly.

In the First step, we'll have to turn AllowEdit property to Yes for BatchJobId field of the Batch table. This is a necessary step to merge all batch tasks into a single batch job. After this step, we'll choose one of the P-Job to be the parent of all, and note down its RecID. The RecId noted will be updated in BatchJobId field for all those separate tasks that belong to different parents which we are just allowed to edit.

Once all the tasks records have updated for the parent Batch job Id field, we can then remove the parent batch job which will contain no more tasks, since we've to merge those tasks into a single parent.

Once the above configurations have been performed, now we'll have to create dependencies of tasks upon each other. For this, we'll open batch job inquiry from System Admin > Inquiries > Batch jobs. Then after choosing the single parent header, we'll have to click View tasks button at the top. This will show all the tasks together into a single place. So whatever task is appearing at first position, will be made leader, and rest as followers in the queue. There will be no change to first task, but from the second task onwards, we'll put condition at the bottom pane, called Has conditions. It does not show any New button to add a record, so just click inside that pane, and press Ctrl+N, to create a new record. Then we'll have to choose the Task Id lookup and select the task before current task, which is leader. Once it is selected Expected status lookup value as Ended or Error will ensure that once the parent task is completed or it has ended with error then only this task will start. Once these settings are done, we'll move to next task, and for that task parent will be 2nd task, and similar settings will be performed so that when the second task is finished or ended with error then the only third task should start. The same will need to be done till the end. Once we have performed these settings then Recurrence pattern for the main task needs to be decided and turn the status to waiting. That's all and now when the main batch job will start all its merged tasks will follow a queue pattern, and will never end up in table deadlocks.

In D365, more flexibility has been added and probably the above settings will not have been tweaked in this way. Rather a task can be added into a batch job easily.

Following screenshot gives an idea how above setup is performed.