Tuesday, 15 November 2011

Quick code example - Event subscription

This is a very quick run-through of a basic event subscription in Ax2012.

There are two classes:
  • MyPostingRoutine is the class that does the main work.
  • PostingLogger provides an event handler that will be attached and run after the posting has completed.

Class MyPostingRoutine

This is the class that the event is attached to. In real terms this could be something like SalesFormLetter, used to post sales documents. In the picture above, you can see that a new event subscription has been attached to the doPost method (by right-clicking the method and selecting new -> post event handler subscription).

static void main(Args args)
    new MyPostingRoutine().run();

public void run()
    setPrefix("Starting process");        
    info("Process has been completed");

private void doPost(str someParameter)
    info("Transaction A posted");
    info("Transaction B posted");
    info("Transaction C posted");        
    info("Posting complete");

Class PostingLogger

This defines a method that does some additional work, such as logging information to an extra table, or possibly sending a document electronically.

The _args passed in (of type XppPrePostArgs) allows the event handler to access the parameters passed into the original function - In this example MyPostingRoutine.doPost.

public static void postHandler(XppPrePostArgs _args)
    str paramFromParentArgs = _args.getArg('someParameter');
    setPrefix("Post logger - Event handler");        
    info(strFmt("Posting information for '%1' logged",paramFromParentArgs));    

Because of the event subscription, method postHandler will be called once doPost has completed.

There's a good series of articles covering this in much more detail at http://daxmusings.blogspot.com/2011/07/ax-2012-models-events-part1-model.html

No comments:

Post a Comment