Calling a new form method from Extension – using CoC – D365 FO

I had a scenario where I had to call a new method in  one of the fields from Vendor Payment Journal Line.
The new method exists in Extension class of  Vendor Payment journal.(which is a form method). This has to be called in modified method of the field ‘Ledger Dimension’.
The steps are as follows
[ExtensionOf(FormStr(LedgerJournalTransVendPaym))]
final class LedgerJournalTransVendPaymFormExt_Extension
{
     public void newMethod()
    {
     ……………….
    }
}
How do call this method in modified ? you have two choices – Create a event handler for the method or Chain of Commands.
On using event handler, I ended up with error when I tried to call the new method using formRun.
[FormDataFieldEventHandler(formDataFieldStr(LedgerJournalTransVendPaym, LedgerJournalTrans, LedgerDimension), FormDataFieldEventType::Modified)]
public static void LedgerDimension_OnModified(FormDataObject sender, FormDataFieldEventArgs e)
{
     FormDataSource ledgerJournalTrans_ds = sender.datasource();
     FormRun formRun= ledgerJournalTrans_ds.formRun();
      formRun.newMethod();
}
Though I did not get any build errors , when navigating from the form I got the error ‘FormRun doesnot have the method ‘newMethod’.
So I went the latter approach and went ahead with creating Chain of Commands.
[ExtensionOf(formDataFieldStr(LedgerJournalTransVendPaym, LedgerJournalTrans , LedgerDimension))]
final class LedgerJournalTransVendPaymField_Extension
{
    public void modified()
    {
         next modified();
        element.newMethod();
     }
}
Note the syntax of creating the extension class for data field in form.
Thus it is developers option to decide whether to use CoC or Event Handler.

 

 

 

 

About AnithaEswaran

Hello all, Thanks for visiting my blog. I strongly believe in "Knowledge increases by sharing ,not by saving". With that in mind, I started this blog to share my learning with D365FO community. Since I am from technical background, most of my posts would be from X++, Azure integration and other topics. Thanks to my mentor and my colleague Romain Gasnier who guided and helped me in learning many new concepts in Ax. This instilled confidence in me to handle and troubleshoot complex issues.
This entry was posted in D365 Operations, Extensions and tagged . Bookmark the permalink.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.