Wednesday, January 14, 2015

Creating batch jobs with X++

Two jobs to create batch jobs with X++, one for runBase and other for SysOperation.
The two jobs are essentially the same, except that the SysOperation job uses both controller and contract classes.
You could also choose to add dependency between two tasks.
batchHeader.addTask(batchTaskToRun);
batchHeader.addTask(dependsOnBatchTask);
batchHeader.addDependency(batchTaskToRun, dependsOnBatchTask);

You will get the same result as here, my previous blog post on adding dependency between jobs manually.

 static void batchRunBase(Args _args)  
 {  
   #define.recurrenceStartTime('23:00')  
   #define.recurrenceInterval(1)  
   #define.noOfRetriesOnFailure(4)  
   BatchHeader                  batchHeader;  
   BatchInfo                    batchInfo;  
   SysRecurrenceData            sysRecurrenceData;  
   utcDateTime                  batchStartTime;  
   Batch                        batch;  
   Tutorial_RunBaseBatch        yourRunBaseBatchClass;  
   ;  
   select firstOnly RecId from batch  
     where batch.ClassNumber == classNum(Tutorial_RunBaseBatch) &&  
       batch.Company == curext() &&  
       batch.DataPartition == getcurrentpartition();  
   if (!batch.RecId)  
   {  
     yourRunBaseBatchClass = Tutorial_RunBaseBatch::construct();  
     batchInfo = yourRunBaseBatchClass.batchinfo();  
     batchInfo.parmRetriesOnFailure(#noOfRetriesOnFailure);  
     // Create the batch job  
     batchHeader = BatchHeader::construct();  
     batchHeader.addTask(yourRunBaseBatchClass); // or controller class  
     //batchHeader.parmCaption("Tutorial");  
     batchStartTime = DateTimeUtil::newDateTime(systemDateGet(), str2time(#recurrenceStartTime), DateTimeUtil::getUserPreferredTimeZone());  
     // Set the recurrence data  
     sysRecurrenceData = SysRecurrence::defaultRecurrence();  
     sysRecurrenceData = SysRecurrence::setRecurrenceStartDateTime(sysRecurrenceData, batchStartTime);  
     sysRecurrenceData = SysRecurrence::setRecurrenceNoEnd(sysRecurrenceData);  
     sysRecurrenceData = SysRecurrence::setRecurrenceUnit(sysRecurrenceData, SysRecurrenceUnit::Day, #recurrenceInterval);  
     batchHeader.parmRecurrenceData(sysRecurrenceData);  
     // Set the batch alert configurations  
     batchHeader.parmAlerts(NoYes::No, NoYes::Yes, NoYes::No, NoYes::Yes, NoYes::Yes);  
     batchHeader.parmStartDateTime(batchStartTime);  
     // Save the batch job  
     batchHeader.save();  
   }  
 }  
 static void batchSys(Args _args)  
 {  
   #define.recurrenceStartTime('23:00')  
   #define.recurrenceInterval(1)  
   #define.noOfRetriesOnFailure(4)  
   BatchHeader                                 batchHeader;  
   BatchInfo                                   batchInfo;  
   SysRecurrenceData                           sysRecurrenceData;  
   utcDateTime                                 batchStartTime;  
   Batch                                       batch;  
   AbcDemandForecastServiceController          AbcDemandForecastServiceController;  
   AbcDemandForecastServiceContract            AbcDemandForecastServiceContract;  
   ;  
   select firstOnly RecId from batch  
     where batch.ClassNumber == classNum(AbcDemandForecastServiceController) &&  
       batch.Company == curext() &&  
       batch.DataPartition == getcurrentpartition();  
   if (!batch.RecId)  
   {  
     AbcDemandForecastServiceController = new AbcDemandForecastServiceController(classStr(AbcDemandForecastServiceOperation), methodStr(AbcDemandForecastServiceOperation, calcDemandForecast), SysOperationExecutionMode::Synchronous);  
     AbcDemandForecastServiceContract = AbcDemandForecastServiceController.getDataContractObject(classStr(AbcDemandForecastServiceContract));  
     batchInfo = AbcDemandForecastServiceController.batchInfo();  
     batchInfo.parmRetriesOnFailure(#noOfRetriesOnFailure);  
     // Create the batch job  
     batchHeader = BatchHeader::construct();  
     batchHeader.addTask(AbcDemandForecastServiceController);   
     //batchHeader.parmCaption("Tutorial SysOperation");  
     batchStartTime = DateTimeUtil::newDateTime(systemDateGet(), str2time(#recurrenceStartTime), DateTimeUtil::getUserPreferredTimeZone());  
     // Set the recurrence data  
     sysRecurrenceData = SysRecurrence::defaultRecurrence();  
     sysRecurrenceData = SysRecurrence::setRecurrenceStartDateTime(sysRecurrenceData, batchStartTime);  
     sysRecurrenceData = SysRecurrence::setRecurrenceNoEnd(sysRecurrenceData);  
     sysRecurrenceData = SysRecurrence::setRecurrenceUnit(sysRecurrenceData, SysRecurrenceUnit::Day, #recurrenceInterval);  
     batchHeader.parmRecurrenceData(sysRecurrenceData);  
     // Set the batch alert configurations  
     batchHeader.parmAlerts(NoYes::No, NoYes::Yes, NoYes::No, NoYes::Yes, NoYes::Yes);  
     batchHeader.parmStartDateTime(batchStartTime);  
     // Save the batch job  
     batchHeader.save();  
   }  
 }