Wednesday 30 January 2013

Microsoft Dynamics AX 2012 Eventing



Microsoft Dynamics AX 2012 Eventing - a look with X++

This will give an overview of eventing with Dynamics AX 2012, further more I have given some examples with Pre, Post and Custom Raised Events to help getting more understanding.

For my example, I'm actually doing straight X++, with no relation to any real world application yet. The idea is to understand what the mechanics are, and then from there apply those in design and in development as needed.

You will notice, in the above project, I have a few classes, called Books & BooksPicker. I think that this would make this a little bit more fun, than the standard hello world!

So let’s take some time, to examine the Books class within my project. 

Here we see the class declaration, in which I have two variables I'm making use of. A Set, called bookCart and an int called book. This is setting up, for later use, within the business logic of my Books class.

Next we move forward to the New method of the Apples class.

With it, we see that I'm setting the variables that we saw in the class declaration, to initialize these variables, for the Set and int types.

Moving forward from there, we go to the next method, called addBooktoCart(). This is where some of the fun starts.

Here we see I'm taking the init book, and adding one to it with the ++; call. Next we move to actually adding an "book" to our bookCart set, with the int of apple as part of the key.

Moving from there, we see a final, what looks to be a method call, that is the following.:

"this.bookCollected(book);"

In looking at this, it would seem as if this was a normal method, and it behaves as such, however this is not a method, but a delegate I created. We can get a peak, inside the delegate I created, for the Books class, with the following screen shot.

Notice that it is blank actually and not doing anything. Its truly a delegate, and looking at this, within the project, we will see that it appears differently, that the rest of the methods within the class.

You can create delegates within the AOT space of a given element, as we see on the Books class in the above image. You do this, as if you were creating a new method.

What you should also notice, is the fact that below it, we have an event handler subscriber that we have created in the same fashion.

Diving deeper into the event handler subscription element, we can see that it has a property page, that allows us to take and set the class, type of, being X++ or Managed, and then the method to call. 

Based on this setup for the event handler subscription element, we can then see it's calling a static method, called pickBook from the BooksPicker class. 

Now it's very important to point out, that all event handler subscription elements must be static methods. These are the only types allowed, for create such an event handler subscriber, or event listener.

So, what this is doing then, is when the Books.addBookToCart() is called, within it, via X++ code, we call to a delegate, of Books.bookCollected(). This delegate, has an event handler subscription setup, for the BooksPicker.pickBook() method.

Let’s now take a look inside that static method, which represents our event subscriber, called pickBook().

As you can see from the method, we are taking a parameter of int bookId, which happens to be the exact method signature of the delegate that is firing this event handler. This method simply takes and calls to the info log, claiming that whatever the id value, is that book has now been picked!

Seeing this in action, as part of this class, I have a job, with the following contents.

In here we see a simple job, that takes and creates a new instance of the Books class, and contains a “for loop” of 10 iterations, of adding books to cart. Since our delegate will be called, after a book is added to the Set, then we see a message similar to the one below, which was fired each time, from the event handler pickBook().

Now moving beyond the mechanics of this process, this can then been applied in several instances of AX. Typically, in other languages, we see such event handlers, as when buttons are clicked, or just before or after a method is fired.

That is actually an option you can do, for any method, you can setup a pre or post event handler subscription for a given method, which will fire on the pre or post processing of another given method.

We can also bring this eventing to a functional level, for say when a Sales Order or greater than $10,000.00 is invoiced, an event is fired that automatically places an order for a pizza party, for the dept. :-)

A little elementary school humor there, but if you really start to think about how this can be used, then the power of such abilities with Microsoft Dynamics AX 2012, comes to life.

Hope this examples will help in understanding the Eventing in AX.

No comments:

Post a Comment