With Microsoft’s premiere ERP system, Dynamics 365 Finance (formerly known as Dynamics 365 for Finance and Operations), you can easily add functionalities thanks to its extensibility. However, there are some guidelines and best practices that must be followed to ensure you’re getting the best out of the solution.

One of the best practices for extensibility is reuse, avoiding the same lines of code in different places.

How can I take advantage of extensibility to write a cleaner code?

Shown below is an example of how to make this happen.

Reduce the number of event handlers linked to various events with the same function.

In most .Net languages (VB.Net or C # .Net) it is possible to link a single event handler to multiple events. A simple scenario might be when several controls have a similar functionality. Also, it is feasible to program a single function that uses the same delegate parameters for these events. The result would depend on the sender object and the event args. This could be a good option if you are looking to reuse the code or reduce the number of methods or functions.

The following scenario explains the procedure to implement it:

We want to implement the same functionality in two table events, when the record is inserted and when it is updated. An ineffective solution is when copying the same code to the two event handlers; therefore, a better procedure is to create a single function in a single event handler.

As shown in the following image, we first need the definition of the event handler:

How Dynamics 365 Finance runs complex schedules with more efficient code extensions

The two events have the same definition, this means that they have the same parameters, a sender object of the Common type and an args parameter of the DataEventArgs type.

To give the function a better meaning, it is recommended that you rename it so that someone can understand the purpose of the function when reading the code.

We can copy the definition of the event by which the event handler is implemented, and paste it separated with a comma (,) inside the square brackets, as shown in the code section highlighted in yellow. This will cause the same event handler to be executed for both events.

Copy to Clipboard

To verify the functionality, we can use the following job, in which there are two events that have been linked to the same previously developed event handler and that should be run twice, when the record is inserted and then when it is updated. The following image shows the job:

Copy to Clipboard

Running the job confirms the double execution of the function. Below you see the result in the infolog:

How Dynamics 365 Finance runs complex schedules with more efficient code extensions

We can get to another level where it is possible to implement the same event handler not only for events of the same object, but also for different objects, as long as they are of the same type. For the example being developed, it is possible to use the Common base class to implement the code.

To demonstrate this in a practical way, the previously developed event handler function is modified.

Copy to Clipboard

Two more events are linked, but for a different object. In this case in the VendTable table. We put the previous job and a more complex code is added to verify that the implementation works for different objects. The function will return an infolog showing the table and the record that was inserted or modified.

Below you see the testing job:

Copy to Clipboard

In the following image we can see the result similar to the first case:

How Dynamics 365 Finance runs complex schedules with more efficient code extensions


Keep in mind that the above example is possible only if the events have event handlers with the same definition: that is, the same types of parameters. The main focus for this implementation should be the sender object. The objects for which this type of implementation is carried out are commonly forms and tables, for forms the sender object are xFormRun types and for tables they are Common types, both are base classes. Event handler parameters should be the basis for programming complex functions for any object and event.

About AlfaPeople

AlfaPeople is a leading Microsoft Dynamics 365 solutions partner with a 10-year track record and operations in more than 14 countries worldwide. With the integral support of our 550+ employees, we offer a range of Microsoft business solutions, such as Dynamics 365 Finance, Dynamics 365 Supply Chain Management, Dynamics 365 Sales, Dynamics 365 Customer Service, as well as a range of support and cloud migration services.

Contact us today to take the step in your business digitization journey!

Development Engineer at AlfaPeople