How to call SysOperation batch job using X++ in D365 F&O

While working on a recent requirement, I needed to trigger a SysOperation framework class directly from code and run it in batch without showing the dialog to the user.

Instead of manually opening the dialog or relying on menu items, I used the controller class to programmatically set the contract values and execute the process in scheduled batch mode. In this post, I will walk through the approach.

Example - 1st 

We create the controller object to initialize the SysOperation framework.

The contract is retrieved and required parameters like ItemId and InventSiteId are set for processing.

The execution mode is set to batch and the dialog is disabled to run the process in the background.


Finally startOperation() triggers the process with all the configured settings.

Code - 

ChangeItemController     controller  = ChangeItemController ::construct();
ChangeItemContract       contract    = controller ? controller.getDataContractObject() : null;
if  (!contract)
{
        throw error(Error::wrongUseOfFunction(funcName()));
}
// Set contract fields
contract.parmItemId(this.ItemId);
contract.parmInventSiteId(this.SiteId );
controller.parmExecutionMode(SysOperationExecutionMode::ScheduledBatch); // Set execution mode
controller.parmShowDialog(false); 
controller.parmLoadFromSysLastValue(false); 
controller.parmInBatch(true);
controller.parmDialogCaption('Change Item');
controller.startOperation();


Example - 2nd

This code programmatically runs the Invent On-hand Storage process in batch mode. It first builds a query filtered by Site and Warehouse using the InventDim table. The query is then encoded and passed to the InventOnhandStorageController through its data contract. The controller is configured to run as a scheduled batch job, without showing the dialog or using last saved values, and finally starts the operation.


Code - 


//Build the query Query query = new Query(queryStr(InventOnhand)); 

QueryBuildDataSource qbds = query.dataSourceTable(tableNum(InventDim)); 

QueryBuildRange qbrWH  = qbds.addRange(fieldNum(InventDim, InventLocationId)); QueryBuildRange qbrSite  = qbds.addRange(fieldNum(InventDim, InventSiteId)); qbrWH.value('WH');

qbrSite.value('SITE'); 


// Create Args object Args args = new Args(); args.name(menuItemActionStr(InventOnhandStorage)); 

args.caller(this); 

InventOnhandStorageController controller = InventOnhandStorageController::construct(args); InventOnhandStorageContract contract    = controller ? controller.getDataContractObject() : null; 


if (!contract) 

    throw error(Error::wrongUseOfFunction(funcName())); 

}


controller.parmInBatch(true); controller.parmExecutionMode(SysOperationExecutionMode::ScheduledBatch); contract.parmViewInventSiteId(true);

contract.parmViewInventLocationId(true); contract.parmQueryString(SysOperationHelper::base64Encode(query.pack())); 

controller.parmShowDialog(false); controller.parmLoadFromSysLastValue(false); controller.batchInfo().parmCaption("@SYS97724"); 

controller.startOperation();

Comments