Skip to main content

Calling a new form method from Extension x++

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.

Comments

Popular posts from this blog

D365 finops send attachment as public URL X++

D365 finops send attachment as public URL X++ A customer needs to download attachments from a mobile application that is connected to Dynamics 365 Finance and Operations (FinOps). I have created the following function in the Service to generate a public URL for the attachments. public str getPublicUrl(RecId _recid) {     DocuRef _ref;     select _ref where _ref.RecId == _recid && _ref.RefTableId == tableNum(PurchTable);     if (_ref.RecId)     {         return File::SendFileToTempStore(             DocumentManagement::getAttachmentStream(_ref),             ERDocuRef_Extension::filename(_ref),             classstr(FileUploadTemporaryStorageStrategy),             true         );     }     else     {         throw error("Attachment not ...

Expired certificates renewal in D365FO

  AX / D365FO – Expired certificates renewal in D365FO Dynamics 365 for Finance and Operations on a local development environment may all of the sudden start reporting problems with the server due to expired certificates. To be sure, you should open the  Event Viewer , expand  Windows Logs  in the left pane and click on the  Application  node. Look for  Warning  logs with source  ASP.NET  and inspect their content. Therefore, when you’ll see  ExpiredCertificateException  under  Exception information , as seen in the image below, you’re dealing with expired certificates. Following the steps below, you will identify and extend required certificates to get access to Dynamics 365 for Finance and Operations again. Step 1: Identify Expired Certificates Start  Windows PowerShell  as administrator and enter the following two commands: cd cert:\LocalMachine\My ls | Select-Object NotAfter,Thumbprint,Subject | Where-Ob...

Error division by Zero

  AX / D365FO – How to prevent the error ‘Division by zero’ If you’re gonna write code like A = B /C, you better make sure that C holds a value. Of course you can do a check with a simple if-statement (if C then A = B/C), but there is an easier way. You can use  minOne , a method that exists in the Global class. int a,b,c; if(b) c = a / b; else print " Cannot divide by zero" OR c = a / c = a / minOne(b) ; //it returns a itself if b is zero because minOne() returns non zero values that we passes. I //it returns a itself if b is zero because minOne() returns non zero values that we passes. I f it is zero then it retuns 1