D365FO - How to create a periodic task

This article explains how to create a periodic task or batch job within Dynamics 365 for Finance and Operations using SysOperationServiceController

As part of this example, I will be creating a simple periodic task that requires parameter input from user. To achieve this we need to create the following elements

  • Data contract class
  • UI Builder class (only recommend if you need custom UI fields)
  • Service class
  • Controller class
  • Action menu item

Data contract

This class is required for handling user data. All parameters defined in this class will be rendered as parameters within the batch job dialog in UI. 

Class declaration


DataContract Attribute
Required to mark this class as a data contract class.

SysOperationContractProcessing Attribute
Specify the UI builder class within the SysOperationContractProcessing attribute to register the UI builder with the data contract (This is only required if you have a UI builder class. This step is optional and can be skipped)

Parameters
  • Declare variables for each parameter within private accessibility.
  • Implement parm methods for each variable that is required for display within the batch job dialog in UI
Parm methods


DataMember Attribute
This is can be specified as is for normal variables. 
If a custom UI field is declared for any variable within the UI builder class then specify the name of the local variable for the parm method to ensure that all user input within the field is synced against this local variable. 

SysOperationLabel Attribute
Only required for fields that has a custom UI field declared within the UI builder class.
Specify the label that will be displayed for the parameter within the batch job dialog in UI.

AifCollectionType Attribute
Only required for fields that has a custom UI field declared within the UI builder class.
Specify the data type of the return object, that is, the type of data that is enable for user input within the batch job dialog field in UI.

Validation


  • Implement SysOperationValidatable if there is a need to validate the parameters. 
  • Implement validate method and include all logic required to validate any parameter
Complete class implementation


UI builder

This class is required for building any custom logic for batch job data fields or creating custom UI fields.

Class declaration
Implements SrsReportDataContractUIBuilder


Variables
  • Specify the contract class as a private variable for accessibility to the contract parm methods
  • Add any new variables for storing the custom dialog field values, for example, DialogField variable with private accessibility. 
Methods

Implement the postBuild method

  • Initialize the contract variable
  • Initialize the dialog field variable and bind it to the contract parm method
  • Specify any other logic required for the dialog field

Implement postRun method

  • This is for specifying the custom lookup for the dialog field. This method can be implemented for any other processing that is required on the dialog field. 
Complete class implementation


Service

This class is required for processing logic after the user has specified all required parameters, all validations have been processed and the batch job is now required to execute. 
Implement all logic for processing data in this class.

Class declaration

Implements SysOperationServiceBase. 



Variables

Specify all variables that are required for processing, for example, the parameters passed from the data contract.

Methods
Specify one method that will serve as the entry point for this class to process data, for example. This will take in the contract as a parameter which will be passed by the controller class. The contract class contains all the data inputs from the user and can be access for process. 

All data processing can then be done thereafter. 

Controller

Create a new controller class that extends SysOperationServiceController. The aim of this class is to handle user data and pass information to the service class for processing. 


  • Implement the description method to specify the name of the batch that will be visible in UI
  • Implement the main method to specify the service class that needs to be executed
  • Specify the method within the service class that needs to be executed, for example, run

Action menu item

To access this within the front-end, create an action menu item that can be added to any existing forms or menus. 


  • Specify an appropriate label
  • Specify the object type as class and select the controller class as object
  • Specify any enum parameters if required
  • Specify the feature class if it exists

Final result



Comments

Popular posts from this blog

D365FO - Create a multi-select lookup in batch jobs

Generate and download a csv file using X++

Creating a batch job with query based filter in D365 back-office