Create parameterized batch job using SysOperationServiceController class Dynamics 365 Finance and Operations

 


Hi, today we will learn how to add a parameter to filter out data using a batch job. We will use create a batch job with the help of using the SysOperationServiceController class. In this article, I will fetch the worker record based on the selected date time through parameter.

No alt text provided for this image

First of all, create a query, add the HcmWorker table in the data source, and drag and drop the created DateTime field into the Range section.

Query

No alt text provided for this image

After this, create three classes (Contract, Service, Controller) and write code as shared below for each class.

No alt text provided for this image


Contract class

[DataContract,SysOperationContractProcessing(classStr(SysOperationAutomaticUIBuilder))
/// <summary>
/// Create getter setter for query
/// </summary>
public class AA_ParameterizeBatchContract
{
    str packedQuery;


    [DataMember,AifQueryTypeAttribute('_packedQuery',queryStr(CfzWokerQuery))]
    public str parmQuery(str _packedQuery = packedQuery)
    {
        packedQuery = _packedQuery;
        return packedQuery;
    }


    public Query getQuery()
    {
        return new Query(SysOperationHelper::base64Decode(packedQuery));
    }


    public void setQuery(Query _query)
    {
        packedQuery = SysOperationHelper::base64Encode(_query.pack());
    }


}]

Service class

/// <summary
/// Create a parameterize batch job 
/// </summary>
public class AA_ParameterizeBatchService extends SysOperationServiceBase
{
    /// <summary>
    ///  Main Action 
    /// </summary>
    /// <param name = "dataContract">AA_ParameterizeBatchContract</param>
    public void startProcess(AA_ParameterizeBatchContract dataContract)
    {
        Query query;


        query = dataContract.getQuery();


        this.executeFlow(query);


    }


    /// <summary>
    /// Fetch the value from the query, Query that return parameter record
    /// </summary>
    /// <param name = "_query">Query</param>
    private void executeFlow(Query _query)
    {


        HcmWorker  workerTable;
        QueryRun   queryRun;


        queryRun =  new QueryRun(_query);


        while(queryRun.next())
        {
            AA_GetWorker         workerRecord;
            workerRecord.clear();
            workerTable.clear();


            workerTable     =  queryRun.get(tableNum(HcmWorker));
           
            select * from workerTable;


            ttsabort;
            workerRecord.AA_WorkerName = workerTable.name();        
            workerRecord.AA_Createdby = workerTable.CreatedBy;
            workerRecord.insert();
            ttscommit;
            
        }
       
      
    }


}>

Controller class

/// <summary
/// Paramerized batch controller class
/// </summary>
public class AA_ParameterizeBatchController extends SysOperationServiceController
{
   
    /// <summary>
    /// Run the Service Opeeration class action
    /// </summary>
    
    protected void new()
    {
        super(classStr(AA_ParameterizeBatchService),methodStr(AA_ParameterizeBatchService, startProcess));


    }


    public static AA_ParameterizeBatchController newFromArgs(Args _args)
    {
        AA_ParameterizeBatchController controllerA     = new AA_ParameterizeBatchController();


        controllerA.batchInfo().parmBatchExecute(true);


        controllerA.initializeFromArgs(_args);


        return controllerA;
    }


    /// <summary>
    /// Main Method
    /// </summary>
    /// <param name = "args">Get records</param>
    public static void main(Args args)
    {
        AA_ParameterizeBatchController controller      = new AA_ParameterizeBatchController();
        
        controller = AA_ParameterizeBatchController::newFromArgs(args);
        controller.startOperation();
    }


    /// <summary>
    /// Filter Dialog
    /// </summary>
    protected void dialogPostRun()
    {
        super();


        IDialogable dlg = this.dialog();
        if(dlg)
        {
            FormRun fr = dlg.formRun();
            if(fr)
            {
                /* If below label does not work for you then write this in double quotes below line --> createddatetime */


                const var fieldName = "@SYS106862";  
                int createDateTime = fr.controlId(fieldName);


                if(createDateTime)
                {
                    FormControl control = fr.control(createDateTime);


                    if(control)
                    {
                        control.enabled(false);
                    }
                
                }
            }
        }
    }


}>

Now build and run your project.

No alt text provided for this image

Happy Learning,

Comments