Tuesday, December 21, 2021

How to add Records to include option on sys operation dialog in D365FO

 Hi guys, Today we see how to add records to include option to sys operation.




While running the batch we have to filter the data based on requirements. Instead of writing while loops. We can use query. Just follow the below steps.


Step 1: Create a static query and add data sources based on your requirement. Here I have added Vendatable.


Required fields drag to Ranges node those will be displayed in the dialog.

Sometimes we need the same data from the query. Instead of selecting the values from the front end, we can define in value property.



Step 2: Create a Contract class, add parm methods for your parameters and we have to add a few methods to get that option.

[DataContractAttribute] class TestSysOperationContract { Name name; str packedQuery; [DataMemberAttribute] public Name parmName(Name _name = name) { name = _name; return name; }

    //SysOperationControlVisibilityAttribute(false)// Hide control

[DataMemberAttribute, AifQueryTypeAttribute('_packedQuery', querystr(TestVendorQuery)) ] 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()); } //public void initQuery() //{ // Query query = new Query(querystr(TestVendorQuery)); // this.setQuery(query); //} }

Step 3: Create a Controller class for calling the service class.

class TestSysOperationController extends SysOperationServiceController
{
    public static void main(Args args)
    {
        TestSysOperationController      controller;
        ;

        controller =  new TestSysOperationController();
        controller.startOperation();
    }
    public void new()
    {
        super(classStr(TestSysOperationService),
                methodStr(TestSysOperationService, process));//,
                    //  SysOperationExecutionMode::Synchronous);
        this.parmDialogCaption("Dialog Title");
    }
} 

Step 4: Create a service class to write our business logic.

class TestSysOperationService extends SysOperationServiceBase
{
    //[SysEntryPointAttribute]
    public void process(TestSysOperationContract _contract)
    {
        QueryRun    queryRun;
        VendTable   vendTable;
        ;

        // create a new queryrun object
        queryRun = new queryRun(_contract.getQuery());

        while(queryRun.next())
        {
            vendTable = queryRun.get(tableNum(VendTable));
            // Your logic.
            info(strFmt('Account - %1 , Name - %2',vendTable.AccountNum,vendTable.name()));
        }
    }

} 

Ends.............

------------------------------------------------------------------------

Sys operation :

If we need to run this batch based on the caller record.


We need to change the logic in the Controller class.

class TestSysOperationController extends SysOperationServiceController { public static void main(Args args) { TestSysOperationController controller; ; controller = TestSysOperationController::newFromArgs(args); controller.startOperation(); } public static TestSysOperationController newFromArgs(Args _args) { TestSysOperationController testController; TestSysOperationContract contract; testController = new TestSysOperationController(); //controller.initializeFromArgs(_args); //contract.initQuery(); if(_args && _args.dataset() == tableNum(VendTable)) { VendTable vendTable; Query query;

            //controller.getDataContractObject()//// for sys operation             //controller.parmReportContract().parmRdpContract() // For Report
contract = testController.getDataContractObject('_contract');
                      // '_contract' this name is from service class method parameter buffer. vendTable = _args.record(); if (vendTable) { query = new query(queryStr(TestVendorQuery));                 //query = contract.getQuery();
//query.clearQueryFilters(); query.dataSourceTable(tableNum(VendTable)).clearRanges(); query.dataSourceTable(tableNum(VendTable)).addRange(fieldNum(VendTable, VendGroup)).value(queryValue(vendTable.VendGroup)); query.dataSourceTable(tableNum(VendTable)).addRange(fieldNum(VendTable, AccountNum)).value(queryValue(vendTable.AccountNum)); contract.setQuery(query); } } return testController; } public void new() { super(classStr(TestSysOperationService), methodStr(TestSysOperationService, process));//, // SysOperationExecutionMode::Synchronous); this.parmDialogCaption("Dialog Title"); } }

multiple records
protected void initContract(Args _args)
{
    MyContract   contract;
    Query        q;
    ;
    contract = this.getDataContractObject('_contract'); 
    q = contract.getQuery();
    
    MultiSelectionHelper multiSelectionHelper;

    multiSelectionHelper = MultiSelectionHelper::createFromCaller(_args.caller());
    multiSelectionHelper.createQueryRanges(q.dataSourceTable(tableNum(MyTable)), 
                                                        fieldStr(MyTable, JournalId));
    
    qbr = findOrCreateRange_W(q.dataSourceTable(tableNum(MyTable)), 
                                                        fieldNum(MyTable, JournalId));

    contract.setQuery(q);
}





Keep daxing!!





No comments:

Post a Comment