Thursday 31 May 2018

D365/ AX 7 - Adding method to table using extension class

D365/ AX 7 - Adding method to table using extension class

AX 7 won’t allow user to add new methods to table or table extension. So here I will be demonstrating how to add new methods to table using extension class.

Lets say our requirement is to add new method to VendTable to get address of Vendors.

First step is to create a class, have a name that has the _Extension suffix and should be static.

public static class VendTable_Extension
{
}

Next Step is to create method, method should always be public static and first parameter should be Table buffer.

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.

class VendTable_Demo
{        
    /// <summary>
    /// job to test vendtable extension method
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {    
        VendTable vendTable;

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



Output:-


No comments:

Post a Comment