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

What does this mean: "The form datasource query object does not support changing its AllowCrossCompany property after the form has executed the query."?

I have made a form with datasources vendtable and vendtrans. Inside vendtable_ds.executequery() looks like this: QueryBuildDataSource queryBuildDatasource ,queryBDS_VendTrans_Invoice; ; queryBuildDatasource = this.query().dataSourceTable(tablenum(vendtable)); queryBDS_VendTrans_Invoice = this.query().dataSourceTable(tablenum(vendtrans)); if (curext() == "MASTERCOMP") { this.query().allowCrossCompany(true); } else { this.query().allowCrossCompany(false); } //FilterVendorName = stringedit control on form if (FilterVendorName.text()) { queryBuildDatasource.addRange(fieldNum(VendTable,Name)).value(strfmt("*%1*", FilterVendorName.text())); } else { queryBuildDatasource.clearRange(fieldNum(VendTable,Name)); } //FilterInvoiceNumber = stringedit control on form if (FilterInvoiceNumber.valueStr() == "") { queryBDS_VendTrans_Invoice.enabled(false); } else { queryBDS_VendTrans_Invoice.enabled(true); queryBDS_VendTrans_In...

Credit Note [Dynamics AX] using X++

This post will help to create credit note for a sales order based on the invent lot id. All the invoices raised for a particular sales line – Lot Id will be raised back as a credit note. Information on Credit Note: A credit note or credit memorandum (memo) is a commercial document issued by a seller to a buyer. The seller usually issues a Credit Memo for the same or lower amount than the invoice, and then repays the money to the buyer or sets it off against a balance due from other transactions Below Code will help to create credit note for all the invoices raised against the sales line -lot id. Please note: This code can be customized as per your requirements. This is just a template to help creating credit note using X++ code. Please test the code before use. static void SR_CreateCreditNote_Sales(Args _args) { // Coded by Sreenath Reddy CustInvoiceTrans custInvoiceTrans; Dialog dialog = new Dialog(“Create credit note – for sales.”); DialogField dfInv...