How to write the event handler in D365

An event handler method is a method that is executed when a specific event occurs on a table, such as inserting, updating, validating, or deleting a record. You can use event handler methods to add custom logic to the standard processes of D365FO.


To write an event handler method for a table, you need to follow these steps:


• Create a new class and name it as <TableName>_EventHandler, where <TableName> is the name of the table that you want to handle events for.


• Add the methods that you want to execute for each event that you want to handle. The methods should have two parameters: Common sender and DataEventArgs e. The sender parameter represents the table buffer that triggered the event, and the e parameter contains information about the event, such as the field name or the data operation type.


• Add the attribute [DataEventHandler(tableStr(<TableName>), DataEventType::<EventType>)] to each method, where tableStr is a global function that returns the name of the table as a string, and DataEventType is an enumeration that specifies the type of event that you want to handle, such as Inserting, Updated, ValidatedField, or Deleted.


Here is an example of how to write an event handler method for a table:


// This is the event handler class

class CustTable_EventHandler

{

    // This method handles the ValidatedField event for CustTable

    [DataEventHandler(tableStr(CustTable), DataEventType::ValidatedField)]

    Public static void CustTable_onValidatedField(Common sender, DataEventArgs e)

    {

        CustTable custTable = sender as CustTable; // Cast the sender to CustTable

        DataEventArgsValidatedField validatedFieldArgs = e as DataEventArgsValidatedField; // Cast the e to DataEventArgsValidatedField

        if (validatedFieldArgs.parmFieldId() == fieldNum(CustTable, AccountNum)) // Check if the validated field is AccountNum

        {

            // Some custom logic for validating AccountNum field

        }

    }


    // This method handles the Inserted event for CustTable

    [DataEventHandler(tableStr(CustTable), DataEventType::Inserted)]    

    public static void CustTable_onInserted(Common sender, DataEventArgs e)

    {

        CustTable custTable = sender as CustTable; // Cast the sender to CustTable

        // Some custom logic for inserting a new customer record

    }

}

Comments

Popular posts from this blog

How to customize electronic reporting in D365

Batch parallelism or multithreading in Dynamics 365 for Finance and Operations

How to create and run the Runbase batch class in D365

How to Enable/Disable a Form Button with X++

Build HTML and send email in D365 FO with X++

Difference between InMemory and TempDB tables in D365 F&O

Conditional Statements in X++

How to apply a package in LCS Dynamics 365 F&O

Customize the standard excel template in D365FO