01.07.2017, 10:13 | #1 |
Участник
|
ievgensaxblog: D365O. Trick to pass a value between Pre and Post event handler using XppPrePostArgs.
Источник: https://ievgensaxblog.wordpress.com/...ppprepostargs/
============== Recently we came across a scenario where we needed to check if a field has changed after super() in update method of a table. Back in the days of AX 2012 you could easily compare original field’s value with current using orig() method before super() and call necessary logic after. X++: public void update() { boolean myFieldHasChanged = this.MyField != this.orig().MyField; super(); if (myFieldHasChanged) { this.doStuff(); } } First option is to use static field, like it’s done in RunBase extension example X++: public static class MyTableEventHandler { private static UnknownNoYes myFieldHasChanged; [PreHandlerFor(tableStr(MyTable), tableMethodStr(MyTable, update))] public static void MyTable_Pre_update(XppPrePostArgs _args) { MyTable myTable = _args.getThis() as MyTable; if (myTable.MyField != myTable.orig().MyField) { MyTableEventHandler::myFieldHasChanged = UnknownNoYes::Yes; } else { MyTableEventHandler::myFieldHasChanged = UnknownNoYes::No; } } [PostHandlerFor(tableStr(MyTable), tableMethodStr(MyTable, update))] public static void MyTable_Post_update(XppPrePostArgs _args) { MyTable myTable = _args.getThis() as MyTable; if (MyTableEventHandler::myFieldHasChanged == UnknownNoYes::Yes) { myTable.doStuff(); } MyTableEventHandler::myFieldHasChanged = UnknownNoYes::Unknown; } } X++: public static class MyTableEventHandler_XppPrePostArgs { const static str myFieldHasChangedArgName = 'myFieldHasChanged'; [PreHandlerFor(tableStr(MyTable), tableMethodStr(MyTable, update))] public static void MyTable_Pre_update(XppPrePostArgs _args) { MyTable myTable = _args.getThis() as MyTable; boolean myFieldHasChanged = myTable.MyField != myTable.orig().MyField; <strong>_args.addArg(MyTableEventHandler_XppPrePostArgs::myFieldHasChangedArgName, myFieldHasChanged);</strong> } [PostHandlerFor(tableStr(MyTable), tableMethodStr(MyTable, update))] public static void MyTable_Post_update(XppPrePostArgs _args) { MyTable myTable = _args.getThis() as MyTable; <strong>boolean myFieldHasChanged = _args.getArg(MyTableEventHandler_XppPrePostArgs::myFieldHasChangedArgName); </strong> if (myFieldHasChanged) { myTable.doStuff(); } } } To overcome this and other limitations of extensions capabilities Microsoft is introducing Method wrapping and chain of command and I’m pretty sure that we’ll see blogs on this from my MVP fellows soon. Источник: https://ievgensaxblog.wordpress.com/...ppprepostargs/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. Последний раз редактировалось mazzy; 02.07.2017 в 09:27. |
|