Thursday 22 February 2018

Extension in D365


To add new method to VendTable to get address of Vendors.

public static class VendTable_Extension

{
    public static Notes getVendAddress(VendTable vendTable)
    {
        DirPartyTable       dirPartyTable;
        LogisticsLocation   logisticsLocation;

        select dirPartyTable where dirPartyTable.RecId == vendTable.Party
        join logisticsLocation
        where dirPartyTable.PrimaryAddressLocation == logisticsLocation.RecId;      
       
        return logisticsLocation.postalAddress();
    }

}

Once we will build our project, this method will be usable as if it is method of VendTable.

I have tested the same using job(Runable Job)

class VendTable_Demo
{        
   
    public static void main(Args _args)
    {    
        VendTable vendTable;

        vendTable = VendTable::find("1001"); 
        
        info(strFmt("Vendor Address - %1",vendTable.getVendAddress()));
      
    }


To get a form control 

formRun.design().controlName(“ControlName”)

or


formRun.design().controlName(formControlStr(FormName, ControlName));

Ex:
[FormControlEventHandler(formControlStr(EcoResProductCreate, CategoryReferenceGroup), FormControlEventType::Modified)]

    public static void CategoryReferenceGroup_OnModified(FormControl sender, FormControlEventArgs e)
{
FormStringControl                   identification_ProductNumber;   //Product number
FormStringControl                   itemIdentification_ItemId;      //Item number

identification_ProductNumber   = formRun.design().controlName("identification_ProductNumber");
       
identification_ProductNumber.text(itemid);
//To call form level method in the extension class
formrun.productData().identification().parmProductNumber(itemid);
}

SysTableLookup in D365

1.Create Extension for form
2.Create New field and copy event handler(OnLookup)




3. Paste event handler in new class and write below logic


[ExtensionOf(formStr(PurchCreateOrder))]
final class PurchTable_Extension
{
    /// <summary>
    /// Added by Rajendra on 01-03-2018
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [FormControlEventHandler(formControlStr(PurchCreateOrder, NEC_RelatedPO), FormControlEventType::Lookup)]
    public static void NEC_RelatedPO_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        SysTableLookup          sysTableLookup;
        Query                   query;
        QueryBuildDataSource    qbds;
        QueryBuildRange         qbr;
        FormRun                 formRun;
        FormStringControl       vendAccount;
       
        formRun        = sender.formRun();
        vendAccount    = formRun.design().controlName(formControlStr(PurchCreateOrder, PurchTable_OrderAccount));
        sysTableLookup = SysTableLookup::newParameters(tableNum(PurchTable), sender);
        //sysTableLookup.addLookupfield(fieldNum(PurchTable, OrderAccount), true);
        sysTableLookup.addLookupfield(fieldNum(PurchTable, PurchId), true);

        query   = new Query();
        qbds    = query.addDataSource(tableNum(PurchTable));
        qbds.addRange(fieldNum(PurchTable, OrderAccount)).value(vendAccount.valueStr());

        sysTableLookup.parmQuery(query);
        sysTableLookup.performFormLookup();

FormControlCancelableSuperEventArgs ce = e as FormControlCancelableSuperEventArgs;


        ce.CancelSuperCall();
       
    }

Please note that execution of original lookup should be cancelled by FormControlCancelableSuperEventArgs.CancelSuperCall() otherwise AX will throw an error “More than one form was opened at once for the lookup control.”


}


2 comments:

  1. Thanks for your informative blog!!! Keep on updating your with such awesome information.
    Microsoft Dynamics AX Online Training

    ReplyDelete
  2. Thank you for sharing this information. I find this information is easy to understand and very useful.
    D365 Finance and Operations Online Training

    ReplyDelete