Creating a Delegate method

Rumman Ansari   Software Engineer   2024-07-13 03:17:44   103  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

The Delegate method is also very helpful to minimize overlaying, and you can use the Delegate method to communicate objects that exist in different packages/models and help to solve dependencies between models when migrating code. Delegate can be very useful when you need to use two objects that are not in the same package. Use the delegate concept by defining a contract between the delegate instance and the delegate handler. We have a new structure of Dynamic 365 for Operations--you will not be able to use an object outside of its own package. So, to use delegate in such situations a delegate declaration must have three things--a Delegate keyword, return type should be void, and it should be an empty method.

Let's understand it using the following recipe.

Getting ready...

To understand this recipe, let's consider a scenario. We have a requirement where we are creating an Expense journal through code and to identify such transactions we added a new field on the LedgerJournalTrans table as ansariNoLedgerPost. Now, whenever users post this journal, it will go to the project journal instead of Ledger accounts.

To skip Ledger posting we need to check for this customized Boolean field and pass a false value to its parmPostToGeneralLedger method.

How to do it...

  1. Add a ProjPostCostJournal class on your solution, and add a new delegate method with the following syntax:
  2. 
        /// <summary> 
        //New delegate method added by Ansari On May 11, 2017 
        /// </summary> 
        delegate void ansariCheckNoLedger(RefRecId _Recid,  EventHandlerResult _result) 
        { 
     
        } 
    

    Delegate: A delegate is a type that represents references to methods with a particular parameter list and return type. In this case, the delegate ansariCheckNoLedger takes two parameters: a RefRecId and an EventHandlerResult.

    Now we have to call this method in New method of ProjPostCostJournal class. Add below code to call delegate method

       
        Boolean noLedger; 
        EventHandlerResult  result = new EventHandlerResult(); 
        if(ledgerJournalTrans.RecId) 
        { 
          this.ansariCheckNoLedger(ledgerJournalTrans.RecId, result); 
          noLedger = result.result(); 
          this.parmPostToGeneralLedger(noLedger); 
        } 
    
  3. Add a new event handler class in your project and add a new method with the following code:
  4. 
     [SubscribesTo(classStr(ProjPostCostJournal),  delegateStr(ProjPostCostJournal, ansariCheckNoLedger))] 
     public static void ProjPostCostJournal_ansariCheckNoLedger(RefRecId _Recid, EventHandlerResult _result) 
         { 
            LedgerJournalTrans ledgerJournalTrans = LedgerJournalTrans::findRecId(_Recid, false); 
            if(ledgerJournalTrans.ansariNoLedgerPost) 
            { 
                _result.result(false); 
            } 
            else 
            { 
                _result.result(true); 
            } 
         } 
    
  5. Now try to post one Expense journal with this scenario and you will see there are no General Journal transactions; all vouchers will post only in Project transactions.

 

How it works...

Delegate methods serve as a contract between the delegate instance and the delegate handler, without any code/logic itself. The SubscribesTo keyword in an event handler method creates a static delegate handler. SubscribesTo requires a class name and delegate method name.

We created a new delegate method and called this method in between the new method. When a pointer calls this method, it will call event handler code and we will get the required buffer.

There's more...

EventHandlerResult is used to get any return value from event handler to use in your code. However, it's not mandatory and you can create as many parameters as required. So, try to add more parameters to explore and play with the Delegate method.

See also

Please have a look at the DimensionHierarchyDelegates class and how it works. This class has many delegate methods and they are invoked from the DimensionFocus form. Try to debug these two objects to better understand the various uses of delegate methods.