Skip to main content

Batch Task Dependency in Dynamics AX 2009

Scenario : You want to schedule 2-3 tasks, but you would like to define dependency among tasks.
Means Task1 should be executed only after completion of Task 3 and Task 2.


Advantage :
You all might have heard of "Divide and Conquer Rule", So this also implements the same.
Generally if you create batch to process 1000 Purchase orders then it might slow down the AOS performance and delay the overall batch performance.
But if you create batch and create 10 tasks, each process 100 Purchase orders then will be fast compared to above procedure and will improve overall batch performance.

How to :
In Dynamics AX 2009, Batch process will make use of below tables

  • BatchJob : Main Job scheduler, consists of task
  • BatchTask : Task in batch which specifies what class to execute in order to achieve certain functionality.
  • BatchConstraint : Used to build dependencies among the tasks.

Create a Job, by using below code

static void Batch_CreateDependency(Args _args)
{
BatchJob batchJob;
Batch batch;
Tutorial_runBaseBatch testDependency;
BatchConstraints batchConstraints;
RefRecId RecId_Task1, recId_task2, recId_task3;

// Create task
RefRecId createTask(str _msg)
{
;
batch.clear();
batch.initValue();
batch.GroupId = '';
batch.BatchJobId = batchJob.RecId;
batch.RunType = BatchRunType::Server;
batch.RetriesOnFailure = 0;
batch.AutomaticTransaction = NoYes::No;
batch.Caption = _msg;
batch.Status = BatchStatus::Waiting;
batch.Company = curext();
testDependency = Tutorial_runBaseBatch::construct();
batch.ClassNumber = classidget(testDependency);
batch.insert();

return batch.RecId;
}

// Building dependencies
void buildDependency(RefRecId _batchId, RefRecid _dependendRecId)
{
;
batchConstraints.clear();
batchConstraints.initValue();
batchConstraints.BatchId = _batchId;
batchConstraints.DependsOnBatchId = _dependendRecId;
batchConstraints.ExpectedStatus = BatchDependencyStatus::FinishedOrError;
batchConstraints.insert();
}

;

batchJob.clear();
batchJob.initValue();
batchJob.OrigStartDateTime = DateTimeUtil::newDateTime(systemDateGet(), DateTimeUtil::time(DateTimeUtil::utcNow()) + 10);
batchJob.Status = BatchStatus::Waiting;
batchJob.Caption = "Test Batch Dependency";
batchJob.insert();

recId_Task1 = createTask("Task 1");
recId_Task2 = createTask("Task 2");
recId_Task3 = createTask("Task 3");

buildDependency(recId_Task1, recId_Task2);
buildDependency(recId_Task1, recId_Task3);

info(strfmt("The %1 Job scheduled to run 3 tasks", batchJob.Caption));
}

When you execute this Job, Go to Basic-->Inquiries-->Batch Job and you should see as below,


As per the snap, you can see Task 1 will be executed once Task 2 and Task 3 gets completed.

thank you friends

Popular posts from this blog

Dynamics Axapta: Sales Orders & Business Connector

Well, again folllowing my same idea of writting close to nothing and pasting code, I'll paste in some code to create a sales order from some basic data and the invoice it. I'll try to explain more in the future. AxaptaObject axSalesTable = ax.CreateAxaptaObject("AxSalesTable"); AxaptaRecord rcInventDim = ax.CreateAxaptaRecord("InventDim"); AxaptaRecord rcCustTable = ax.CreateAxaptaRecord("CustTable"); rcCustTable.ExecuteStmt("select * from %1 where %1.AccountNum == '" + MySalesOrderObject.CustAccount + "'"); if (MySalesOrderObject.CurrencyCode.Trim().Length == 0) MySalesOrderObject.CurrencyCode = rcCustTable.get_Field("Currency").ToString().Trim(); string sTaxGroup = rcCustTable.get_Field("taxgroup").ToString().Trim(); //set header level fields axSalesTable.Call("parmSalesName", MySalesOrderObject.SalesName.Trim()); axSalesTable.Call("parmCustAccount", M

Passing values between form and class

Class name is EmplDuplication and Form is EmplTable . void clicked() {    MenuFunction mf;    args args = new Args();    ;     args.record(EmplTable);     mf = new menufunction(identifierstr(EmplDuplication), MenuItemType::Action); mf.run(args); } Meanwhile, in the main() method of the EmplDuplication class, we need to put this Axapta x++ code to get the datasource: static void main(Args args) {     EmplDuplication EmplDuplication; EmplTable localEmplTable; ;     if(args.record().TableId == tablenum(EmplTable)) localEmplTable = args.record();     ... }