
How Dynamics 365 Finance runs complex schedules with more efficient code extensions
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:
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.
[fusion_syntax_highlighter theme=”oceanic-next” language=”javascript” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]W0RhdGFFdmVudEhhbmRsZXIodGFibGVTdHIoQ3VzdFRhYmxlKSwgRGF0YUV2ZW50VHlwZTo6SW5zZXJ0ZWQpLCAKCkRhdGFFdmVudEhhbmRsZXIodGFibGVTdHIoQ3VzdFRhYmxlKSwgRGF0YUV2ZW50VHlwZTo6VXBkYXRlZCldIAoKcHVibGljIHN0YXRpYyB2b2lkIEN1c3RUYWJsZV9vbkluc2VydGVkX2FuZF9vblVwZGF0ZWQoQ29tbW9uIHNlbmRlciwgRGF0YUV2ZW50QXJncyBlKSAKCnsgCgogIGluZm8oIkV4ZWN1dGVkIG9uIGluc2VydGVkIGFuZCB1cGRhdGVkIik7IAoKfQ==[/fusion_syntax_highlighter]
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:
[fusion_syntax_highlighter theme=”oceanic-next” language=”javascript” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]cHVibGljIHN0YXRpYyB2b2lkIG1haW4oQXJncyBfYXJncykgCgp7ICAgICAgICAgCgogIEN1c3RUYWJsZSBjdXN0VGFibGU7IAoKICBOYW1lIG5hbWUgPSAiSnVhbiBQw6lyZXoiOyAKCgoKICBjdXN0VGFibGUuQWNjb3VudE51bSAgICA9ICJVUy0wMDA5OSI7IAoKICBjdXN0VGFibGUuQ3VzdEdyb3VwICAgICA9JzMwJzsgCgogIGN1c3RUYWJsZS5DdXJyZW5jeSAgICAgID0nVVNEJzsgCgogIGN1c3RUYWJsZS5QYXltVGVybUlkICAgID0nTmV0MTAnOyAKCiAgY3VzdFRhYmxlLlBheW1Nb2RlICAgICAgPSdDSEVDSyc7IAoKICBjdXN0VGFibGUuaW5zZXJ0KERpclBhcnR5VHlwZTo6T3JnYW5pemF0aW9uLCBuYW1lKTsgCgoKCiAgdHRzYmVnaW47IAoKCgogIGN1c3RUYWJsZS5zZWxlY3RGb3JVcGRhdGUodHJ1ZSk7IAoKICBjdXN0VGFibGUuUGF5bVRlcm1JZCA9ICdOZXQzMCc7IAoKICBjdXN0VGFibGUudXBkYXRlKCk7IAoKCgoKCiAgdHRzY29tbWl0OyAKCgoKfSA=[/fusion_syntax_highlighter]
Running the job confirms the double execution of the function. Below you see the result in the infolog:
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.
[fusion_syntax_highlighter theme=”oceanic-next” language=”javascript” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]W0RhdGFFdmVudEhhbmRsZXIodGFibGVTdHIoQ3VzdFRhYmxlKSwgRGF0YUV2ZW50VHlwZTo6SW5zZXJ0ZWQpLCAKCkRhdGFFdmVudEhhbmRsZXIodGFibGVTdHIoQ3VzdFRhYmxlKSwgRGF0YUV2ZW50VHlwZTo6VXBkYXRlZCksIAoKRGF0YUV2ZW50SGFuZGxlcih0YWJsZVN0cihWZW5kVGFibGUpLCBEYXRhRXZlbnRUeXBlOjpJbnNlcnRlZCksIAoKRGF0YUV2ZW50SGFuZGxlcih0YWJsZVN0cihWZW5kVGFibGUpLCBEYXRhRXZlbnRUeXBlOjpVcGRhdGVkKV0gCgpwdWJsaWMgc3RhdGljIHZvaWQgQ3VzdF9WZW5kX1RhYmxlX29uSW5zZXJ0ZWRfYW5kX29uVXBkYXRlZChDb21tb24gc2VuZGVyLCBEYXRhRXZlbnRBcmdzIGUpIAoKeyAKCiAgQ3VzdFRhYmxlIGN1c3RUYWJsZTsgCgogIFZlbmRUYWJsZSBWZW5kVGFibGU7IAoKCgogIGlmIChzZW5kZXIuVGFibGVJZCA9PSB0YWJsZU51bShDdXN0VGFibGUpKSAKCiAgeyAKCiAgICBjdXN0VGFibGUgPSBzZW5kZXIgYXMgQ3VzdFRhYmxlOyAKCiAgICBJbmZvKHN0ckZtdCgiVGhlIEN1c3RvbWVyICUxIHdhcyBpbnNlcnRlZCBvciB1cGRhdGVkIiwgY3VzdFRhYmxlLkFjY291bnROdW0pKTsgCgogIH0gCgogIGVsc2UgaWYgKHNlbmRlci5UYWJsZUlkID09IHRhYmxlTnVtKFZlbmRUYWJsZSkpIAoKICB7IAoKICAgIFZlbmRUYWJsZSA9IHNlbmRlciBhcyBWZW5kVGFibGU7IAoKICAgIEluZm8oc3RyRm10KCJUaGUgVmVuZG9yICUxIHdhcyBpbnNlcnRlZCBvciB1cGRhdGVkIiwgVmVuZFRhYmxlLkFjY291bnROdW0pKTsgCgogIH0gCgoKCgoKfSA=[/fusion_syntax_highlighter]
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:
[fusion_syntax_highlighter theme=”oceanic-next” language=”javascript” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]cHVibGljIHN0YXRpYyB2b2lkIG1haW4oQXJncyBfYXJncykgCgp7ICAgICAgICAgCgogIEN1c3RUYWJsZSBjdXN0VGFibGU7IAoKICBWZW5kVGFibGUgdmVuZFRhYmxlOyAKCgoKCgogIHR0c2JlZ2luOyAKCgoKICBjdXN0VGFibGUgPSBDdXN0VGFibGU6OmZpbmQoJ1VTLTAwMDk5JywgdHJ1ZSk7IAoKICBjdXN0VGFibGUuUGF5bVRlcm1JZCA9ICdOZXQzMCc7IAoKICBjdXN0VGFibGUudXBkYXRlKCk7IAoKCgoKCiAgdHRzY29tbWl0OyAKCgoKICB2ZW5kVGFibGUuQWNjb3VudE51bSAgICA9ICJWZW5kLTAwMDk5IjsgCgogIHZlbmRUYWJsZS5WZW5kR3JvdXAgICAgID0nMzAnOyAKCiAgdmVuZFRhYmxlLkN1cnJlbmN5ICAgICAgPSdVU0QnOyAKCiAgdmVuZFRhYmxlLlBheW1UZXJtSWQgICAgPSdOZXQxMCc7IAoKICB2ZW5kVGFibGUuUGF5bU1vZGUgICAgICA9J0NIRUNLJzsgCgoKCiAgdmVuZFRhYmxlLmluc2VydCgpOyAKCgoKCgp9IA==[/fusion_syntax_highlighter]
In the following image we can see the result similar to the first case:
Conclusion
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!