Skip to main content

Using XppPrePostArgs to pass parameters through a method

· 2 min read
Kome Hoang
Maintainer of Automaly

References

How to

Take a very safe table to play around: CustGroup (Customer Group), we will see how we can utilise XppPrePostArgs here to save a parameter before the update is commited to the database and then take it out for us to execute futher logic.

The tricky part is that such table does not even have an update method.

Normally saying, if a method is available on the common, we can navigate to the design of it and copy the Pre- and Post-Event Handler from here.

Alt text

Yet, in our example here, such method - update - is not available. So, we can simply make a Class extension to wrap the common.update() method. The code can be as simple as below.

[ExtensionOf(tableStr(CustGroup))]
final class CustGroup_Extension
{
public void update()
{
next update();
}
}

We will then take the original value of CustGroup.Name and save it to the instance of XppPrePostArgs. This part will be under the Pre-Event method. The code is as below.

const static str OrigNameFld = "OrigName";

[PreHandlerFor(classStr(CustGroup_Extension), methodStr(CustGroup_Extension, update))]
public static void CustGroup_Extension_Pre_update(XppPrePostArgs args)
{
// Get custGroup buffer
// No super() has been called, so the changes have not been committed
CustGroup custGroup = args.getThis();

// Get original value of field Name
Description origName = custGroup.orig().Name;

// Save the value to args
args.setArg(
OrigNameFld
, origName
);
}

After the changes are commited to the database, we will take the saved parm out and execute our logic. In this case, I will assess if the new CustGroup.Name contains "Kome", if it does, revert it back to its original value and throw message. This part will be under Post-Event method. The code is as below (I will show the whole class so we can see how the parm is passed by using setArg and getArg)

internal final class CustGroup_EventHandlers
{
const static str OrigNameFld = "OrigName";

[PreHandlerFor(classStr(CustGroup_Extension), methodStr(CustGroup_Extension, update))]
public static void CustGroup_Extension_Pre_update(XppPrePostArgs args)
{
// Get custGroup buffer
// No super() has been called, so the changes have not been committed
CustGroup custGroup = args.getThis();

// Get original value of field Name
Description origName = custGroup.orig().Name;

// Save the value to args
args.setArg(
OrigNameFld
, origName
);
}

[PostHandlerFor(classStr(CustGroup_Extension), methodStr(CustGroup_Extension, update))]
public static void CustGroup_Extension_Post_update(XppPrePostArgs args)
{
// Get custGroup buffer and its column Name after updated
CustGroup newCustGroup = args.getThis();
Description newName = newCustGroup.Name;

// Assess and revert
if(Global::strContains(newName, "Kome"))
{
ttsbegin;
{
// Get the saved parm here and set it to the field value
newCustGroup.Name = args.getArg(OrigNameFld);
newCustGroup.doUpdate();
}
ttscommit;

Global::info(strFmt("New description - \"%1\" - contains \"Kome\". Reverted to its original.", newName));
}
}
}

And, so, we can test it out by setting a new Description for a Customer group...

Alt text

...upon clicking on button Save, the original value will be updated and a message will be thrown.

Alt text