Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table Update Event Handler

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.

like image 984
AnthonyBlake Avatar asked Oct 04 '13 10:10

AnthonyBlake


1 Answers

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.

like image 105
Jan B. Kjeldsen Avatar answered Oct 16 '22 09:10

Jan B. Kjeldsen