NAV2018: xRec in OnModify

Every now and then a developer takes the xRec bait and they’re wondering why their code doesn’t work and it usually ends up with a lot of lost time combined with a good amount of frustration.
I hope most of you do know that if you trigger the OnModify through code and xRec is used in the OnModify trigger, Rec and xRec will be the same, however it works fine when it’s triggered through the UI.

In NAV2017 and before this was ‘solved’ by a workaround: a function called OnModify with a record parameter called xRec.
Meaning you would have to either save the record in another variable before changing values or retrieve the xRec from the database and then call the OnModify function and another MODIFY(TRUE) to actually apply the changes instead of a plain and simple Rec.MODIFY(TRUE).

Let’s have a look at the implementation in NAV2017 taking codeunit 5056 and table 5050 as an example:
The OnModify trigger of table 5050 (just calling the OnModify function with xRec)

Updating a contact from a customer in codeunit 5056:

The workaround works but it’s still a bit tricky and there’s a big chance you’ll forget all of this when your head is in the code.

In NAV2018 it’s still a workaround but now it’s a proper one:
The OnModify function is still there and in the OnModify trigger of the table Rec and xRec are compared as text and if they’re equal then it will most likely be the case that the OnModify trigger has been triggered by code.
Then all there’s left to do is retrieve xRec from the database by using a FIND followed by a call to the OnModify function.
Even the OnModify function is completely obsolete and can be removed, but for some reason all the calls to this function are not (yet) refactored to the MODIFY(TRUE).


Leave a Reply

Your email address will not be published. Required fields are marked *