Table Update Event Handler

点点圈 提交于 2020-01-13 01:55:14

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/19179250/table-update-event-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!