RSS

When does xrec work?

13 May

As a Business Central developer we have from time to got used to using xrec and rec as container for previous value and current new value.

However, there are certain scenarios when xrec doesn’t work as intended.

I had a scenario where I was debugging and wanted to know where a value was changed in code. To make it easier I used the trick to do a subscriber event on the OnAfterModify trigger on that record and put a break point there to see xrec and rec and where it was changed.

Quickly I discovered that xrec and rec showed the same value. Then I remember that xrec doesn’t always work and a trick is to get the record instead with record.get to get the value before it gets written. So I changed my code and got this.

Wait, all of these are the same, even the item.get. Then it hit me… 🤦‍♂️ As you can see we are subscribing to the OnAFTERModify event which takes place AFTER the new value has been written to database.

Ok, then I tried the OnBeforeModify event instead and got the following result:

Here we see that the xrec doesn’t work on OnBeforeModify BUT record.get works!

I took this to twitter and AJ Kauffmann pointed out that xrec only works in your code when your are assigning a field through validate and use the event OnBeforeValidate. I tried that and it gave me the following result:

So when assigning a value with record.validate and listening to OnBeforeValidate event you get the xrec value.

Further on it was pointed out that xrec only works as intended on pages so I had to test that as well which gave the the following result:

And it is right, xrec works as intended on pages with OnAfterModifyEvent.

Conclusion

It is dangerous to use xrec if you don’t know when it actually works. As we can see xrec only works with the OnBeforeValidate event when you are assigning through validate with your own code. Otherwise it only works on pages.
For more in depth explanation of this I recommend this blog post “How to get a reliable xRec” by AJ.

 
Leave a comment

Posted by on 2023-05-13 in Okategoriserade

 

Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
The BC Docs Librarian

Tips and news for AL programmers

Stefan Maroń

Dynamics 365 Business Central

James Pearson

Thoughts about Dynamics 365 Business Central, Git, Azure DevOps, PowerShell, Automated Testing, Visual Studio Code...and maybe some other stuff

Roberto Stefanetti BLOG

MVP & MCT Microsoft Dynamics 365 Business Central, Powershell, Azure, SQL Server

JoeBrown

DevOps starts with culture!

Comments for DevOps ABCs Blog

Reflections from the ERP field

DevOps ABCs Blog

Reflections from the ERP field

Dynamics 365 Business Central for Partners

Reflections from the ERP field

Freddys blog

Learn something new... - then Teach someone!

Stefano Demiliani Technical Blog

Reflections from the ERP field

David Worthington's NAV Performance Blog

Micosoft Dynamics NAV Performance Testing & Optimization

Freddys Blog

Reflections from the ERP field

Marije Brummel Blog | Business Central, Azure and more...

Design Patterns, Performance, Clean Code

Vjeko.com

Ideas in the cloud

Goprowe AB

Reflections from the ERP field

ArcherPoint

Reflections from the ERP field

The NAV Viking´s Blog

- Sharing my experience in the Dynamics NAV world

Comments for

Reflections from the ERP field

Nav Can Be Fun

Its Really Fun to Work with Microsoft Dynamics Nav / D365 Business Central

%d bloggers like this: