In the previous post we were talking about a solution that would allow us to customize the content and structure of the payload that is sent through Business Events drastically, so that we are able to adapt to a JSON format different from the standard of #MSDyn365FO, all this while still using the full power of this framework. Now, let’s move on to see in more detail the steps taken to reach this solution.
As we mentioned in the last post, we will divide this solution into two different parts:
- Identify the events that must comply with the new format.
- Modify the payload to send abroad.
In this specific article, we will talk about the technical aspects and the steps carried out to be able to fulfill the first point.
Identify events in the event catalog
In order to correctly identify which events must comply with the specific format, we have decided to “mark” them within the event catalog.
When we launch the functionality that is responsible for generating (or regenerating) the business event catalog, the system goes through all the classes that are decorated with the BusinessEventsAttribute attribute and generates a record in the event catalog (BusinessEventsTable) for each of the classes obtained (events).
The decision that has been taken to identify those business events that meet these new conditions was none other than to imitate the standard functionality, for this reason, a new attribute was generated that will be responsible for decorating these specific events, indicating the specific type of event (data requested in the new format), so that, when generating or regenerating this event catalog, we could obtain all the classes decorated with the new attribute and mark them somehow within the event catalog.
In order to meet this objective, the following steps were carried out.
1. Add 2 new fields to the BusinessEventsTable table.
To do this, we create the extension of the BusinessEventsTable.JATCustomEvent table, and add the fields:
- JATCustomEvent: NoYes enum that will indicate whether or not that business event belongs to our catalog of custom events.
- JATEventType: String that will store the specific event type.
2. Create new attribute to decorate custom business events.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class JATCustomBusinessEventAttribute extends SysAttribute { private str eventType; public void new(str _eventType) { super(); eventType = _eventType; } public str getEventType() { return eventType; } } |
To generate the new attribute, you basically have to create a class that inherits from the SysAttribute. Besides, we add a private property to be able to indicate in the attribute itself (through the new method) the type of event in question, and a method that will allow obtaining the associated value later.
3. Decorate our business event with the new attribute.
1 2 3 4 5 6 7 8 9 |
[JATCustomBusinessEvent('Project.ERP.InvoiceCreated'), BusinessEvents(classStr(JATInvoiceBusinessEventContract), "@JATCustomEvent:InvoiceEvent", "@JATCustomEvent:InvoiceEventDesc", ModuleAxapta::Customer)] public class JATInvoiceBusinessEvent extends BusinessEventsBase { ... } |
As you can see, apart from decorating our business event with the BusinessEventsAttribute attribute, we add the attribute that we have created, indicating by parameters the type of event, a value that will be added later to the payload.
(If you need more details about how to create new business events, you can get more information in this post).
4. Mark the events as “custom” within the event catalog.
For this last step, we are going to use the BusinessEventsCatalogHelper class, since it is in charge of going through the different business events of the system and creating as many records in the BusinessEventsTable table as business events there are in the system. Specifically, we are going to extend the addCodeBusinessEvents method, which is in charge of carrying out this management, adding at the end (after the next) the code necessary to update our events.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
[ExtensionOf(classStr(BusinessEventsCatalogHelper))] final class BusinessEventsCatalogHelperJATCustomEvent_Extension { public static void addCodeBusinessEvents() { BusinessEventsTable businessEventsTable; next addCodeBusinessEvents(); Array customEvents = new Array(Types::Integer); customEvents = DictClass::getAttributedClasses(classStr(JATCustomBusinessEventAttribute)); ttsbegin; for (int i = 1; i <= customEvents.lastIndex(); i++) { SysDictClass businessEventDictClass = new SysDictClass(customEvents.value(i)); JATCustomBusinessEventAttribute businessEventsAttribute = businessEventDictClass.getAttribute(classStr(JATCustomBusinessEventAttribute)); BusinessEventsId businessEventId = businessEventDictClass.name(); select firstonly forupdate businessEventsTable where businessEventsTable.BusinessEventId == businessEventId; if (businessEventsTable.RecId) { businessEventsTable.JATCustomEvent = NoYes::Yes; businessEventsTable.JATEventType = businessEventsAttribute.getEventType(); businessEventsTable.update(); } } ttscommit; } } |
What we do in this method is to get all the events (classes) that are decorated with the attribute that we created in step number 2 and update the corresponding record of the BusinessEventsTable table so that it will be marked with the value of the JATCustomEvent field to “Yes” and will have the value of the event type assigned in the JATEventType field.
With these modifications we would already fulfill the objective of this article, to identify business events so that, as we will see in the next post, we are able to modify the structure of the message sent only for those events that we need.
PS: This code will be executed every time someone launches the action from the button Rebuild business events catalog or every time a database synchronization is performed.
1 comment / Add your comment below