How to: Modify Parameter Values in a Pre-Method Event Handler [AX 2012]

Updated: October 7, 2011

Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

In Microsoft Dynamics AX, you can assign a static method to be an event handler that starts and ends immediately before a specific method on a class ends. This is called a before-method event handler, or a pre-method event handler. In the AOT you assign the event handler as a node under the method node. The CalledWhen property on the event handler node must be set to Pre.

The before-method event handler can modify the parameter values passed by the caller method, before the called method receives them.

For more information, see Event Handler Nodes in the AOT.

The following table displays the AOT elements that work together to process the event.

AOT element type

X++ Code sample

Plain method: as a method on the TestClass class.

// Called by the job later in this example.
public str formatWholeName
        (str _firstName, str _lastName)
{
    return _lastName + ", " + _firstName;
}

Event handler: static method on a class.

// CalledWhen = Pre.
static public void formatWholeNameEhBefore(XppPrePostArgs ppArgs)
{
    str firstName;
    //
    firstName = ppArgs.getArg("_firstName");

    if ("Dave" == firstName)
    {
        ppArgs.setArg("_firstName", "David");
    }
    else if ("Bill" == firstName)
    {
        ppArgs.setArg("_firstName", "William");
    }
}

Node relationship in the AOT.

In the following image, notice that the formatWholeName method node has a child node for the TestClass::formatWholeNameEhBefore event handler.

AOT event handler hierarchies

AOT nodes for a method and its event handler

Job: to run the simple method.

static void Job1NameEhBefore(Args _args)
{
    TestClass testClass9;
    str formattedWholeName;
    //
    testClass9 = new TestClass();

    // Run a method that has a pre-method event handler,
    // one that starts and ends before the method starts.
    formattedWholeName = testClass9.formatWholeName
        ("Dave", "Ahs");

    // The Infolog displays the effect of the before-method event handler.
    info(strFmt
        ("%1 == the formatted formal name.",
        formattedWholeName));
}

/*** Output displayed in the Infolog:
Ahs, David == the formatted formal name.
***/


Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.

Community Additions

ADD
Show: