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

Mark All for Open Cust Trans and Open Vend Trans

We have situations where there are lots of open transactions that need to be settled against each other. This can be the case if auto settlement is turned off. One solution is to add a "Mark All" button to the custOpenTrans or vendOpenTrans forms. This button "checks" the mark checkbox on every line. The user can then uncheck several lines if needed and Update to settle the lines. The code below is an example of what we used on the open vendor transaction screen. The code is very similar on the AR side. One note: I used vendTable.AccountNum in the code below. That should be generalized to work with any buffer that is passed into the open trans form. void customMarkAll() { VendTransOpen localVendTransOpen; VendTrans localVendTrans; container conSum; int linesProcessed; ; //show wait cursor startLengthyOperation(); element.lock(); //remove all prior markings specOffsetVoucher.deleteSpe...

Print Report in Microsoft Dynamics AX 2009 through X++

I am trying to print sales confirmation report on a button click which I have added on Sales Order Detail form in Microsoft Dynamics AX 2009. On click event of that button, I have written following code: void clicked() {     Args                args;     ReportRun           reportRun;     SalesFormLetter     salesFormLetter;     PrintJobSettings    printJobSettings;     CustConfirmJour     custConfirmJour;     RecordSortedList    list                = new RecordSortedList(55);     SalesTable          salesTableUpdate;     ;     SELEC...

Creating Free Text Invoice through X++ code

Job: Calling class from job to run the class public void freeTextInvoicePostTestJob() { Dialog dialog; DialogField dlgCustAcc; DialogGroup dialogPeriodLengthGroup, dialogPeriodLengthGroup1; DialogField dlgLedgerAcc; ; dialog = new Dialog("Free-Text Invoice"); dialogPeriodLengthGroup1 = dialog.addGroup('Cust Table'); dlgCustAcc = dialog.addField(typeid(CustAccount)); dialogPeriodLengthGroup = dialog.addGroup('Ledger Table'); dlgLedgerAcc = dialog.addField(typeid(LedgerAccount)); if(dialog.run()) { if(dlgCustAcc.value() && dlgLedgerAcc.value() != '') FreeTxtInvoiceCreatePost::main(dlgCustAcc.value(), dlgLedgerAcc.value()); else throw error(strfmt("Either CustAccount or LedgerAccount info is missing.")); } } Class: Which creates the free text invoice class FreeTxtInvoiceCreatePost { } static void main(CustAccount _custAccount, LedgerAccount _ledgerAccount) { CustInvoiceTable custInvoiceTable; ...