In dynamics AX 2012, customization on tables, forms, and classes was done by simply overriding methods on them, But In Dynamics 365 for Operations, the preferred mechanism for customizations to existing objects is to use event handlers to react to various events rather than overriding methods on tables, forms, and classes.
If you are a beginner or new to dynamics 365 Finance & Operations, you can use Microsoft's Free Virtual Machine. Learn How to Set Up a Free Virtual Machine for Dynamics 365 Development
For example, to change what happens when a button is clicked in AX 2012 you override the clicked method and put code either before or after the super() call. In Dynamics 365 for Operations, you can react to the OnClicked event by copying the event handler method for the event and pasting the method into a class.
Below is an example of an event handler method that reacts to the OnClicked event of a button on a form and controls the behavior of methods by using two types of events, proceeding Event ( that occurs before the method's super() is called) and succeeding Event (that occurs after the method super() is called).
With D365 event handlers play a very important and hence we need to know how and when to use them to maximum benefit. Also for Improving the Development Efficiency and Performance of D365 Applications Use of an event handler is always recommended, and it is the safest way to modify any existing functionality in Dynamics 365 for Finance and Operations. However, it may not fit every requirement, but always try to use an event handler in every possible place.
You can use an event handler on Classes, Forms, and Tables. On any method, whether it's on a Table, Class, or Form, you can write pre or post-event handler, while on Tables and Forms you will also get standard Events under Event nodes such as onInserting, onDeleted, and so on.
To understand this concept better, let's take the example of On Inserting Event Handler with Table. When we use event handlers on the basis of your selection criteria, the event handler method will execute. For example, if you choose post Event handler, it will execute just after that method execution is finished. Refer to the following code to understand post Event handler.
Post-event handler is the same, but the only difference is that it will execute after the parent method. Apart from pre-and post-event handlers, Dynamics 365 for Finance and Operations provides event handling on system events/methods such as onDeleting, OnDelete, OnInsert, and so on.
These event handlers have a different syntax than the Pre-Post event handler, but the concept is the same. To use these event handlers, simply expand the Events node on Tables and Forms. Classes don't have events. Expand the Events node and choose the required event, then right-click and select Copy Event Handler Method and paste it into your event handler class.
Example
For a better understanding of the concept let us take one example of the CustTable event handler. After saving a customer record we have to change the customer group automatically through x++ code.
- Open the CustTable in the designer window.
- Expand the Events node.
- Right, Click on the OnInserted method and select Copy Event Handler Method.
- Then Create one class in your project give the name EventHanlderCustTable.
- Paste event handler code inside the class.
class EventHandlerCustTable
{
[DataEventHandler(tableStr(CustTable), DataEventType::Inserted)]
public static void CustTable_onInserted(Common sender, DataEventArgs e)
{
CustTable custTable = sender as CustTable;
// 90 is the Customer group code for Intercompany Cutomer
if(custTable.CustGroup=="90")
{
// 80 is the Customer group code for Other Customer
custTable.CustGroup="80" ;
Global::error("Created Customer is an Intercompany Cutomer and will be added to the Customer Group Other Customer");
// you can write your logic here.
}
}
}
Now build the code and run it, you will see whenever some record is inserted into CustTable. You can test your code, for this go to Account Receivable > All Customers. While creating the customer if you are choosing the Customer Group code as "90", it will show the message given inside Globale:: error method as shown below. The example given here is not a standard one. You can write your own logic as per the requirements.
Conclusion
In Microsoft Dynamics 365 Finance & Operation writing customization by using event handlers is improving the development efficiency and performance. this is the safest and fastest way of modifying any existing functionality, even though it may not be fit for all situations, always use event handlers in every possible place. If this post is useful share it with your friends.
Happy coding with D365 Snippets 🙂.
Event Handler Methods in Dynamics 365 for Operations
What is the purpose of an event handler?
0 Comments:
Post a Comment