Event Handlers D365fo
Event Handlers D365fo
Event Handlers D365fo
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.
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 control the behaviour of methods by
using two types of events, precedding Event ( that occurs before the method's super() is called)
and succedding Event (that occurs after the method's super() is called).
With D365 event handlers play very important and hence we need to know how and when to use
them to maximum benefit. Also for Improving Development Efficiency and Performance of
D365 Applications Use of 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 with every requirement, but always try to use event handler in every possible place. You
can use 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 using 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 handler, 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 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. Classesdon't have Events. Expand the Events node and choose the required event, then
right-clickand select Copy Event Handler Method and paste it into your event handler class.
Example
For better understanding of the concept let us take one example of CustTable event hanlder.
After saving a customer record we have to change the customer group automatically through x+
+ code.
1. Open the CustTable in designer window.
2. Expand the Events node.
3. Right Click on the OnInserted method and select Copy Event Handler Method.
4. Then Create one class in your project give the name EventHanlderCustTable.
5. Paste event handler code inside the class.
Now lets say we have a scenerio in which we need to default some value in table's field before
insertion takes place, we will modify the code as shown below:
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");
}
Now build the code and run, you will see whenever some record is inserted into CustTable. You
can test your code , for this go to Account Receivlable > 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 standared 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 develoment 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 places. If this post is usefull share to your friends.