Why we should use events
If it’s not clear to you yet what the actual benefit of eventing is I would like to note that in AX 2012 already the first and most important one is upgrade complexity and in the end upgrade cost. Code modifications do need to be merged potentially, event subscriptions don’t. I know that saying so simplifies things a little…
Secondly there were some changes made in the structure of the application that make a transition to eventing mandatory in some cases. I’ll cover this topic later with a separate blog post (or likely several) but for reference the keyword here is Packages.
Types of events
There are three types of “events” you can subscribe to in AX7:
- Standard events
The biggest change here comes with the Events nodes you find in the designer for a lot of object types. Microsoft introduced loads of standard events for the relevant phases of execution. As an example in AX 2012 you needed to modify or at least pre- or post-event the update() method of a table to extend its behavior. Both still is possible but in addition to that there are two events you can subscribe to, onUpdating() and onUpdated().
Delegates were there before and still are but subscribing to them changed. What changed and is said to be an ongoing process in the future is that Microsoft adds delegates in certain places to enable us to extend the behavior of things (without having to modify standard code). An random example would be that in class WmsLocationTreeBase there’s a delegate for the change of the tree so you can subscribe and react to that event:
delegate void treeChanged()
You can still add pre- and post-events to any method. Just the way to do it changed.
Subscribe to events and find subscriptions
To create a new subscription in all three cases you can simply right-click the event source, standard event / method / delegate, in the designer of the object and click Copy event handler method. This copies the code for a valid and well-formed subscription method to your clipboard. You only need to create a new event handler class (or use an existing one, of course) and paste the code there which then looks like this (for a table named SomeTable’s event onUpdated()):
public static void SomeTable_onUpdated(Common sender, DataEventArgs e)
As you can see there are attributes and parameters; those differ between event types, of course.
To find existing event subscriptions you can simply right-click the publisher in question and Find event handlers to display all the subscribers in the Find Symbol Results area immediately.
Be aware of the execution order! Getting back to the example there’s a difference betweenonUpdated() and the post-event to the update() method. Let me explain it with a little example. Having an update() method like this
public void update()
that has a pre-event as well as a post-event has the following execution order:
- Pre-event subscriptions
- Subscriptions to onUpdating()
- super() call
- Subscriptions to onUpdated()
- Post-event subscriptions