Table Update Event Handler
Asked Answered
N

1

5

I am investigating the capabilities of the new delegate & event subscription pattern in AX 2012.

At the moment I am looking to detect when a particular field has been modified, for example when SalesTable.SalesStatus is changed to SalesStatus::Invoiced.

I have created the following post-event handler and attatched to the SalesTable.Update method;

public static void SalesTable_UpdatePosteventHandler(XppPrePostArgs _args)
{
    Info("Sales Update Event Handler");
}

Now I know I can get the SalesTable from the _args, but how can I detect a field has changed? I could really use a before & after version, which makes me think I am subscribing to the wrong event here.

Naivete answered 4/10, 2013 at 10:40 Comment(2)
Probably not your answer but can you detect changes using the salesTable.orig() values? msdn.microsoft.com/en-us/library/xrecord.orig.aspxKarlsruhe
@Karlsruhe I dont think it works because I am looking after the updateNaivete
G
9

If the update method does not update the field, you can use a pre event handler on the update method. If you want to monitor the PriceGroup field on the CustTable table then create a class called CustTableEventHandler containing this method:

public static void preUpdateHandler(XppPrePostArgs _args)
{
    CustTable custTable = _args.getThis();
    if (custTable.PriceGroup != custTable.orig().PriceGroup)
        info(strFmt("Change price group from '%1' to '%2'", custTable.orig().PriceGroup, custTable.PriceGroup));
}

A post event handler will not work, as orig() will return the changed record. Also if the the record is updated using doUpdate your handler is not called.

You could also override the aosValidateUpdate on CustTable, which is called even if doUpdate is used. This method is always run on the AOS server.

public boolean aosValidateUpdate()
{
    boolean ret = super();
    if (this.PriceGroup != this.orig().PriceGroup)
        info(strFmt("Change price group from '%1' to '%2'", this.orig().PriceGroup, this.PriceGroup));
    return ret;
}

Yet another option would be a global change to the Application.eventUpdate method. From the header of the method:

Serves as a callback that is called by the kernel when a record in a table is updated, provided that the kernel has been set up to monitor records in that table.

A developer can set up the kernel to call back on updates for a given table by inserting a record into the DatabaseLog kernel table with all fields set to relevant values, which includes the field logType set to EventUpdate. It is possible to set up that the kernel should call back whenever a record is updated or when a specific field is updated.This is very similar to how logUpdate is called and set up. The call of this method will be in the transaction in which the record is updated.

This method is used by the alert rule notification system. I would recommend against this, unless it is a global change (like alert rules).

Alert rules can be extended as described here.

Godchild answered 7/10, 2013 at 12:17 Comment(2)
nice answer Jan I will get back to you when I have tried a few of these outNaivete
Jan, you pointed me in the right direction. You may wish to add this to the last art of your answer as I am using is extensively msdn.microsoft.com/en-us/library/aa673670.aspx - also, I will leave the bounty open until it expires just in case!Naivete

© 2022 - 2024 — McMap. All rights reserved.