Setting up Batch jobs using X++ [Dynamics AX]

Friends, I know there is no point in setting up the batch jobs using X++ code when we can do it easily through batch setups.

However, If someone is interested in knowing the process or the code to setup server side batchable jobs and setting up recurrence and intervals using x++, below is the code.

Note: In the below code, I have used the class Tutorial_runbaseBatch as a safe side as it doesnt contain any business logic to execute. Once you run the job you will find the class getting added to the queue and same can be seen from Basic >> Batch job list – user

static void SR_SetupServerBatchJobs(Args _args)
{
BatchHeader header;
SysRecurrenceData sysRecurrenceData;
Batch batch;
BatchJob batchJob;
Tutorial_RunBaseBatch tutorial_rbb;
BatchInfo processBatchInfo;
BatchRetries noOfRetriesOnFailure = 4;
;

// Create the tutorial_RunBaseBatch job, only if one does not exist
select batch where batch.ClassNumber == classnum(Tutorial_RunBaseBatch);
if(!batch)
{
// Setup the tutorial_RunBaseBatch Job
header = BatchHeader::construct();
tutorial_rbb = Tutorial_RunbaseBatch::construct();
processBatchInfo = tutorial_rbb.batchInfo();
processBatchInfo.parmRetriesOnFailure(noOfRetriesOnFailure);
header.addTask(tutorial_rbb);

// 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(Tutorial_RunBaseBatch);

sysRecurrenceData = batchJob.RecurrenceData;
sysRecurrenceData = conpoke(sysRecurrenceData, 8, [2]);
batchJob.RecurrenceData = sysRecurrenceData;
batchJob.update();
ttscommit;
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: