Raju's Dynamics AX / Dynamics 365 Finance and Operations Blog

BLOG BY RAJU. DYNAMICS AX , DYNAMICS 365 FINANCE AND OPERATIONS EXPLORER

How to get record in Pre Insert method in Table Extension Class. D365FO;Dynamics 365 Finance and Operations

Example: You have a Table : VendTable. Now you want to write some code that needs to be executed before insert method. How to do that and how to get the record in that method.

  1. Create an Extension Class for VendTable. Ex: ABVendTable_Extension
  2. Now Goto AOT > DataModel>Tables>VendTable. Right Click on table and click Open
  3. Now Expand Methods on Table > Right Click on Insert > Click Copy Event Handler Method > Click Pre-Event Handler.
  4. Now Paste this into the Extension class that we created in Step 1.
  5. So Inside code of that method, if you want to get the record the code will be like this as shown below.
  6. The if loop in below record is something i have written as i have created two fields and if i enter 30 in VendorRank, it should change value on other string field to Thirty. This is just an example i took to use pre insert method.

[ExtensionOf(tablestr(VendTable))]
Public static class ABVendTable_Extension
{

/// <summary>
///
/// </summary>
/// <param name="args"></param>
[PreHandlerFor(tableStr(VendTable), tableMethodStr(VendTable, insert))]
public static void VendTable_Pre_insert(XppPrePostArgs args)
{
    VendTable VendTable = args.getThis(); //Code to get record.


    if(VendTable.VendorRank == 30)
    {
        VendTable.VendorRankName = "Thirty";
    }
   // args.setReturnValue(VendTable); 
}

}

Published by

Leave a comment

Design a site like this with WordPress.com
Get started