How to create and run the Runbase batch class in D365

In this article, you will learn how to create and run a batch class that extends RunBaseBatch in D365. A batch class is a class that can be scheduled to run periodically or on demand on the Application Object Server (AOS) or a client. A batch class must extend the RunBaseBatch class, which provides the methods and properties that the system needs to run the class as a batch job.


Steps to create a batch class:


1. Open the Application Object Tree (AOT) in a development workspace and create a new class under the Classes node. Give it a meaningful name, such as MyBatchClass.


2. In the class declaration, add extends RunBaseBatch to indicate that your class inherits from the RunBaseBatch class.


3. Override the run method of the RunBaseBatch class and write your business logic in it. This is the method that will be executed when the batch job runs. For example, you can write code to query and update some records in a table.


4. Override the pack and unpack methods of the RunBaseBatch class and write code to serialize and deserialize your class members. These methods are used to store and retrieve the state of your class when it is run as a batch job. 


For example, you can use the SysPackExtensions class to append and find your class members in a container.


5. Optionally, you can override other methods of the RunBaseBatch class, such as dialog, getFromDialog, initParmDefault, description, canGoBatchJournal, etc., to customize the behavior and appearance of your batch class. 


For example, you can use the dialog method to create a dialog box that prompts the user for some parameters before running the batch job.


Steps to run a batch class:


1. Open an application workspace and go to System Administration> Inquiries > Batch jobs > Batch jobs.


2. Click New to create a new batch job.


3. In the Batch job form, enter a name and description for your batch job.


4. Click View tasks to add one or more tasks to your batch job. A task is an instance of a batch class that will be executed by the batch job.


5. In the Batch tasks form, click Add to create a new task.


6. In the Task description field, enter a name for your task.


7. In the Class name field, select your batch class from the list.


8. Click OK to close the Batch tasks form.


9. Optionally, you can modify the properties of your batch job, such as recurrence, alerts, priority, etc., by clicking Functions > Change status or Functions > Batch job.


10. Click OK to close the Batch job form and save your batch job.


11. To run your batch job immediately, click Functions > Batch processing and select Batch processing enabled. Then click OK.


12. To run your batch job later or periodically, click Functions > Batch processing and select Batch processing enabled and Recurring job enabled. Then specify the recurrence pattern and range in the Recurrence form. Click OK twice.


For example: 


Here is the code for extending the run base batch class:






You can copy the code here:

public class TNDemoRunBase extends RunBaseBatch 

    DialogField     dialogAccount; 

    DialogField     dialogFromDate; 

    DialogField     dialogToDate; 

    CustAccount     custAccount; 

    FromDate          fromDate; 

    ToDate               toDate; 

    #DEFINE.CurrentVersion(1) 

    #LOCALMACRO.CurrentList 

        custAccount, 

        fromDate, 

        toDate 

    #ENDMACRO 


    protected Object dialog() 

    

        DialogRunBase     dialog; 

        DialogGroup         groupPeriod; 

        dialog = super(); 

        dialogAccount     = dialog.addFieldValue(extendedTypeStr(CustAccount), custAccount);

        groupPeriod        = dialog.addGroup("Period"); 

        dialogFromDate  = dialog.addFieldValue(extendedTypeStr(FromDate), fromDate, "Period from"); 

         dialogToDate      = dialog.addFieldValue(extendedTypeStr(ToDate), toDate, "Period to"); 

        return dialog; 

    

    public boolean getFromDialog() 

    

        boolean     ret; 

        ret = super(); 

        custAccount = dialogAccount.value(); 

        fromDate = dialogFromDate.value(); 

        toDate = dialogToDate.value(); 

        return ret;

    }

    public container pack() 

    

        return [#CurrentVersion,#CurrentList]; 

    

    public boolean unpack(container _packedClass) 

    

        Version version = RunBase::getVersion(_packedClass); 

        switch (version) 

        

            case(#CurrentVersion) : 

                [version,#CurrentList] = _packedClass; 

            break; 

            default : 

                return false; 

        

        return true;

    }

    static client server ClassDescription description() 

    

        return "Sum customer transactions"; // it should be label id

    }

    public void run() 

    

        CustTrans     custTrans; 

        select sum(AmountMST) 

            from custTrans 

                where custTrans.AccountNum == custAccount 

                && custTrans.TransDate >= fromDate 

                && custTrans.TransDate <= toDate; 

        info(strFmt("Sum equals %1", custTrans.AmountMST));

    

    static void main(Args _args) 

    

        TNDemoRunBase demoRunBase; 

        demoRunBase = new TNDemoRunBase(); 

        if (demoRunBase.prompt()) 

        

            demoRunBase.run(); 

        

    }





Comments

Popular posts from this blog

How to customize electronic reporting in D365

Batch parallelism or multithreading in Dynamics 365 for Finance and Operations

How to Enable/Disable a Form Button with X++

Build HTML and send email in D365 FO with X++

Difference between InMemory and TempDB tables in D365 F&O

Conditional Statements in X++

How to apply a package in LCS Dynamics 365 F&O

Customize the standard excel template in D365FO

How to write the event handler in D365