Conditional Batch Jobs

By - August 8, 2013

Batch jobs are a convenient way to process resource-heavy tasks without locking up a client session.  Most batch jobs are single-task executions initiated from a Periodic menu item.  However, a user can also manually configure a custom batch job with multiple tasks and conditions to create complex batch flows.

As an example, we will create a custom AIF processing flow, illustrated in the following flowchart.   In summary, we will retrieve our XML files and process those inbound files. Depending on the results of the inbound processing, we’ll either proceed to process outbound files, or output error response files.  Additionally, when outbound processing completes, we’ll also output response files.

Keep in mind that the business logic behind each individual task is not important, and the main idea of this exercise is to control the flow of execution as each task completes.  The screenshots that follow are from Microsoft Dynamics AX 2009, but this configuration also applies to Microsoft Dynamics AX 2012.

Conditional Batch Jobs-pic1

 

Batch Tasks

Basic > Inquiries > Batch job

To start, we’ll create a new empty batch job, by creating a Batch job record.  Then we’ll use the View tasks button to add tasks in the Batch tasks form.

Conditional Batch Jobs-pic2

 

We add each task as a new record in the Batch tasks form.  Each of these tasks is a RunBaseBatch class already capable of running on the batch server.  The lower Has conditions grid lists the conditions that must be met before the highlighted task can execute.  Tasks are specified in the condition record by their Task Id, so good Task descriptions are recommended for complicated batch flows.

Conditional Batch Jobs-pic3

 

Task A – AIF Receiving

This task retrieves the AIF XML files from a file directory.

No conditions:  Task A is the first task in the flow, no conditions are needed.  Because this task has no conditions, there is nothing preventing it from starting when the batch job executes.

Conditional Batch Jobs-pic4

 

Task B – AIF Inbound Processing

This task processes any inbound files that were retrieved in Task A.

Execute if Task A ended: Task B will only execute after Task A completes with the Expected status of “Ended”.

Conditional Batch Jobs-pic5

 

Task C – AIF Outbound Processing

This task processes the outbound portion of AIF.

Execute if Task B ended: Task C will only execute if Task B completes with the Expected status of “Ended”.

Conditional Batch Jobs-pic6

 

Task D – AIF Sending

This task outputs queued outbound files to a file directory.

Execute if Task B errored: Task D will execute if Task B completes with the status of “Error”.  This would happen if an exception is thrown in the business logic of Task B.

Execute if Task C ended or errored: Task D will also execute if Task C completes with either the “Ended” or “Error” status.

Conditional Batch Jobs-pic7

Notice that the Condition type is set to “Any”, which means Task D will execute if either of the above two conditions are met.

 

With the batch job configured as described above, we were able to functionally control the execution of tasks and add logic to our batch job without writing any code.  Utilizing conditions on batch tasks further extends the utility of the Dynamics AX batch framework, and helps businesses attain more value from their ERP.

 

 

 

Receive Posts by Email

Subscribe and receive notifications of new posts by email.