Monday, 28 September 2020

How to schedule a batch job of a class with parameter from X++

 Hi Folks,

sometime we received  a requirement to schedule a batch from x++ code runtime. This kind of requirement comes when there is performance issue while running the same class from previous execution.

For example, we need to write a code to update some data while posting a journal. We want journal posting logic to schedule a batch job for update. In this way journal posting will happen separately and it will schedule a batch job which will do the update and execute other business logic from batch service.

Let say I have a service class which accept a contract class parameter.

// service class

class GSPaymentSettlementService extends SysOperationServiceBase

{

    [SysEntryPoint]

    public void paymentSettlement(GSPaymentSettlementContract     _contract)

    {

        // Your business logic 

        }

}


// contract class

[DataContract]

class GSPaymentSettlementContract

{

    Id       id;

    [DataMember, SysOperationControlVisibility(false)]

    public Id parmId(Id _id = id)

    {

        id = _id;

        return id;

    }

}


Now you want to write a logic to schedule a batch job to call the service class and pass the parameter.

you can write below code to achieve this.


            BatchHeader                         batchHeader;

            SysOperationServiceController       controller;

            GSPaymentSettlementContract     dataContract;

            str                                 captiontext;

            

            captiontext = "Procee the bi for " + <passing value>;

            batchHeader = BatchHeader::construct();

            batchHeader.parmCaption(captiontext);


            //create instances of the classes to use as batch tasks

            controller = new SysOperationServiceController( classStr(GSPaymentSettlementService),

                                                            methodStr(GSPaymentSettlementService, paymentSettlement),

                                                            SysOperationExecutionMode::Synchronous);


            controller.parmLoadFromSysLastValue(false);

            dataContract = controller.getDataContractObject('_contract');

            dataContract.parmId(<passing value>);

            batchHeader.addTask(controller);

            batchHeader.save();


// try this it comment me if it provide you a solution.

1 comment:

D365FO Entity insert/update issue Update not allowed for field ‘Entity.FieldName’.

Step 1: Make sure fields properties allow edit is yes in Data entity level(In Table level Allow edit is No). Change to Yes in entity fields ...