Udostępnij za pośrednictwem


How to: Modify the Return Value in an Post-Method Event Handler

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

You can assign a static method to be an event handler that starts when a specific method on a class ends. This is called an after-method event handler, or a post-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 Post.

The post-method event handler can modify the return value of the completed method, before the return value is given to the method caller.

For background information about event handler nodes, see Event Handler Nodes in the AOT.

The AOT Elements

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

AOT element type

X++ Code sample

Plain method: as a member of a class.

public int numberOfExpectedAttendees
        (int _numOfInvites, int _numOfDeclines)
{
    return _numOfInvites - _numOfDeclines;
}

Event handler: starts as a method of a class.

// CalledWhen = Post.
static public void numberOfExpectedAttendeesEhAfter
        (XppPrePostArgs ppArgs)
{
    int intAttendees;
    //
    intAttendees = any2Int(ppArgs.getReturnvalue());
    info(strFmt("%1 = intAttendees raw, in event handler.", intAttendees));
    if (0 > intAttendees)
    {
        intAttendees = 0;
        ppArgs.setReturnValue(intAttendees);
    }
}

Node relationship in the AOT.

In the following image, notice that the numberOfExpectedAttendees method has a child node for the numberOfExpectedAttendeesEhAfter event handler. This node is created by dropping a static method

AOT event handler hierarchies

AOT nodes for a method and its event handler

By coincidence, the method node for the numberOfExpectedAttendeesEhAfter method is also on this same TestClass class.

Job: to run the simple method.

static void Job2ReturnTestEhAfter(Args _args)
{
    TestClass testClass9;
    int attendeeCountReturned;
    //
    testClass9 = new TestClass();
    // Run a method that has an event handler that starts after the method finishes.
    attendeeCountReturned = testClass9.numberOfExpectedAttendees(8, 13);
    // The Infolog output displays the effect of the after-method event handler.
    info(strFmt
        ("%1 == the number of expected attendees.",
        attendeeCountReturned));
}
/*** Output displayed in the Infolog:
0 == the number of expected attendees.
***/

See also

How to: Modify Parameter Values in a Pre-Method Event Handler

Walkthrough: Subscribing an Event Handler to a Delegate in the AOT

Event Handler Nodes in the AOT

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