Controller in SysOperation framework in D365 F&O

Long Answer
Views 204

Answer:

Controller in SysOperation framework in D365 F&O

Controller: As the name implies, this class has great responsibility for initiating the operation. This class holds all the information regarding execution mode; it should show a progress form or dialog. It is best practice not to write the whole business login in the Controller class itself. That's why, in this demo, we have created one service class to write our business logic, and that service class reference is provided in this controller class main method.


        class CustBalanceController extends   
         SysOperationServiceController 
        {  
           str packedQuery; 
           CustBalanceDataContract contract; 
     
 
          /// <summary> 
          /// Sets the query ranges based on caller. 
          /// </summary> 
          /// <param name="_query"> 
          /// The hold the <c>Query</c> object of the service. 
          /// </param> 
          public void setRanges() 
          { 
             QueryBuildRange         queryBuildRange; 
             QueryBuildDataSource    queryBuildDataSource; 
             FormDataSource          custTableDS; 
             CustTable               custTable; 
             str                     range; 
             Query                   _query; 
 
            contract = this.getDataContractObject() as   
             CustBalanceDataContract; 
            _query   = contract.getQuery(); 
            if (this.parmArgs() 
            &&  this.parmArgs().caller() 
            &&  this.parmArgs().dataset() == tableNum(CustTable)) 
             {   
                custTableDS = FormDataUtil::getFormDataSource  
                 (this.parmArgs().record()); 
 
                  if (_query && custTableDS) 
                 {  
                    // build range 
                    for (custTable = custTableDS.getFirst(true) ?
                    custTableDS.getFirst(true): custTableDS.cursor(); 
                    custTable; 
                    custTable = custTableDS.getNext()) 
                    { 
                       range = range == '' ? custTable.AccountNum :
                       range  
                       + ',' + custTable.AccountNum; 
                    } 
 
                        if (range) 
                     {    
                        queryBuildDataSource =   
                         _query.dataSourceTable(tableNum(CustTable)); 
 
                        // check for QueryBuildDataSource 
                        if (queryBuildDataSource) 
                       {   
                          // clear the old range,and then add it 
                            queryBuildDataSource.clearRanges(); 
                        if (!queryBuildRange) 
                        {      
                           queryBuildRange   
                           =queryBuildDataSource.addRange
                            (fieldNum(CustTable, AccountNum)); 
                        } 
                         
                        queryBuildRange.value(range); 
                       } 
                     } 
                 } 
             } 
                contract .setQuery(_query); 
          } 
 
            public static void main(Args _args) 
            {  
               CustBalanceController   controller = new 
               CustBalanceController(classStr(CustBalanceService), 
               methodStr(CustBalanceService,processData),
               SysOperationExecutionMode::Synchronous); 
 
               controller.parmArgs(_args); 
               controller.setRanges(); 
               controller.startOperation(); 
            } 
 
        } 

Here, we extend the SysOperationServiceController class to inherit controller capabilities. The main method is used to create an instance of the controller class, where we specify the service class and service method which need to be called to execute the business logic. The setRanges() method is called to specify ranges based on the caller.

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of X++ Programming Language, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.