Blog

Name is Anant Dubey and the intent to create this blog is to discuss the problems and issues that developer face in the dynamics AX development and to share the new things that come up with the new version of AX.

Monday, November 14, 2016

How to create and debug batch job in AX 2012

There are many scenario where we have to schedule the tasks, so they execute in background. Real world scenarios are
  • Sales order with certain criteria will update status to Invoiced.
  • Scheduled job check the file location to find comma delimited files and after finding create sales order or purchased orders in Ax.
  • Schedule job clear the data in database logging after certain time.
  • Schedule job execute mid night to extract all sales order/ Purchase order and integrated it other system.

In dynamics Ax we can schedule the with help of RunBaseBatch framework.

For current example, I just wrote the batch job which just put its execution time in custom table.
First step for batch process is to enable the Dynamics Ax 2012 as Batch server.
Link


Enable Server
Consider a custom table with only two fields. These fields just dummy text and the time when batch process execute.

Table Structure

Now just create a class, set it run at server.

Class attributes
Extends the class with run batch base class.  The class logic should be same


class ProcessTableIncrement extends RunBaseBatch

{

 

}

 

public container pack()

{

return conNull();

}

 

public void run()

{

 

// The purpose of your job.

TblBatchHit _hit;

 

_hit.HitBy ="BatchJob";

_hit.hitTime= DateTimeUtil ::utcNow();

_hit.insert();

}

 

public boolean unpack(container packedClass)

{

return true;

}

 

Now compile the code and Generate Increment CIL. Incremental CIL generation is required to setup every time you update the code.

Now create ax job which deploy above class as batch Process job.

static void TestBatchHit(Args _args)

{

BatchHeader header;

SysRecurrenceData sysRecurrenceData;

Batch batch;

BatchJob batchJob;

ProcessTableIncrement _ProcessIncrement;

BatchInfo processBatchInfo;

BatchRetries noOfRetriesOnFailure = 4;

;

 

// Create the tutorial_RunBaseBatch job, only if one does not exist

select batch where batch.ClassNumber == classnum(ProcessTableIncrement);

if(!batch)

{

// Setup the tutorial_RunBaseBatch Job

header = BatchHeader::construct();

_ProcessIncrement = new ProcessTableIncrement();

processBatchInfo = _ProcessIncrement.batchInfo();

processBatchInfo.parmRetriesOnFailure(noOfRetriesOnFailure);

processBatchInfo.parmCaption("Table Increment");

 

header.addTask(_ProcessIncrement);

 

// Set the recurrence data

sysRecurrenceData = SysRecurrence::defaultRecurrence();

SysRecurrence::setRecurrenceStartDateTime(sysRecurrenceData, DateTimeUtil::addSeconds(DateTimeUtil::utcNow(), 20));

SysRecurrence::setRecurrenceNoEnd(sysRecurrenceData);

SysRecurrence::setRecurrenceUnit(sysRecurrenceData, SysRecurrenceUnit::Minute);

header.parmRecurrenceData(sysRecurrenceData);

// Set the batch alert configurations

header.parmAlerts(NoYes::No, NoYes::Yes, NoYes::No, NoYes::Yes, NoYes::Yes);

header.save();

 

// Update the frequency to run the job to every two minutes

ttsbegin;

select forupdate batchJob

join batch

where batchJob.RecId == batch.BatchJobId

&& batch.ClassNumber == classnum(ProcessTableIncrement);

 

sysRecurrenceData = batchJob.RecurrenceData;

sysRecurrenceData = conpoke(sysRecurrenceData, 8, [3]);

batchJob.RecurrenceData = sysRecurrenceData;

batchJob.update();

ttscommit;

}

}

 

 
When you execute the X++ job, you will find a new batch job created and waiting state at following link with caption Table Increment

Job Link


From top menu you can check the job execution history.

Batch Job

Job History

If any error occur or you put some info logs, these can be seen from Log from top menu.
For example in some other batch job I used many info boxes at different locations to trace.
11-12-2014 12-58-09 AM

Remove / Delete the batched job.
Delete the existing job is two-step process if job is in waiting step. First we have to convert it into withhold state and then delete it.
Select the required batch job for example if I want to delete Job with caption “Table Increment”. From top menu to select change state to with hold

select

withhold

Now again go in Functions top menu and select

delete
Now from dialog set status to withhold and click on ok
ddd
This will remove the Batch Job

Debug the Batch Process Job
Now question is how we can debug batch job, for that purpose we have to go in visual studio.

For debugging at AOS server following check box must be check form AOS server configuration.
“Enable breakpoints to debug x++ code running on this server on this machine”
X++



If Client and AOT are in same machine then open the Application explore and locate the file and attach debug there.
Visual studio debug


If files are at AOS is another machine go at XPPIL folder in visual studio and add break point.



From debug menu click on attached process
Attached process
From  Attach to select the manage code.
From top click on select button and select the Managed(v4.0) code
ttss
Check the two check boxes below “Show Processes from All Users and show processes in all sessions and click on refresh.
After refresh select Ax32Serv.exe and click on attach button.

When the job execute after certain time link appear on break point.
sss

Reference taken by:- https://community.dynamics.com/ax/b/alirazatechblog/archive/2014/11/12/exploring-the-batch-job-process-in-dynamics-ax-2012-r3

No comments:

Post a Comment