We all know Eventing is a great functionality in AX 2012. It allows you to decouple your logic from base code from Microsoft, which provides your business ease of upgrades in the future.
However, as Marcos wrote back in 2011, it only works as well as the base (aka application) code was designed to work with event handlers. I’ll give you an example.
Say you want to extract the logic on a Sales Order’s Sales Status field. You’d like to have a single place where all of the beginning, in-process, and ending statii (statuses) and their transitions reside. This is not possible without a lot of engineering, as the application has buried this in several places throughout the system.
An answer to this problem would be to take a look at the KanbanFlowStateMachine class. It fully extracts the statii, the starting, in process, and ending points, and based on a container of conditions can help transition from point A to point B.
I’ve taken this class a bit further, so that I don’t have to re-create the wheel so to speak. Below is a simple interface IStateful which mimics the Kanban class’s major functions:
Next, I implement this interface in an abstract class called Stateful that performs the bulk of the work. It is designed using anyType so that we can work with any status field in the system:
However, being abstract this class is not to be instantiated directly. To hook in to this, I’ve created an example ItemGroupStateMachine class which extends Stateful:
The ItemGroupStateMachine class has a single purpose, and that is to fully construct the necessary start, in-process, and ending states and how to transition between them. The InventItemGroup table does not have a status field by default, so I’ve created a new enum ItemGroupApproval:
Getting back at what Marcos wrote a few years ago, now that the base application code has been designed to support event-driven logic, we now can harness delegates on the Stateful class, which publishes messages out to our various subscribers:
Now, writing a quick job, I can instantiate a new ItemGroupStateMachine, and ask it to transition to the next state given some fake conditions. We’ll see it successfully moves to the next status, but it also triggers our event handler which can execute additional logic on status change. This is the power of AX 2012!