Hi All,

Today wanted to share - how to write delegates in Dynamics 365.

A delegate declaration must have three things:

  • The delegate keyword
  • Type void
  • Empty method

Delegate method always should be of type "Void". To access the result value we have to pass EventHandlerResult object as a parameter for the delegate method. Delegate methods doesn't contain any code.

delegate void <method name>_delegate(<Input parameter from the actual method>, EventHandlerResult _result)

{

}

Below is the example:

In the standard AX Class > JournalFormTable > designNameLookupJournalName() , required to add additional range to the standard query. So in this scenario, i have used the delegates concept to add additional range to the query.

Step 1: Create a new method in the Class > JournalFormTable and name it,

In our case creating the delegate method for the "designNameLookupJournalName()" so named it as "designNameLookupJournalName_delegate". Below is the code snippet.

Step 2: Right click on the delegate method and select "copy event handler method"

Step 3: Create a new class and paste the copied event handler from step 2.

[SubscribesTo(classStr(JournalFormTable), delegateStr(JournalFormTable, designLookupJournalName_delegate))]
public static void JournalFormTable_designLookupJournalName_delegate(QueryBuildDataSource _qbds, EventHandlerResult _result)
{

}

 

Step 4: Now we should be able to add the logic in method from step 3. 

this method is having 2 parameters "QueryBuildDataSource, EventHandlerResult".

- QueryBuildDataSource : Since we need to add additional range so "QueryBuildDataSource" as input parameter from the original method. Below is the code snippet with range logic in it.

Step 5: Call this delegate method in the standard method with required parameters 

So in this case we can create the delegate methods for adding the additional logic to the standard AX methods without disturbing the existing standard logic.

Regards,

Naresh Kumar Kolli.