RecordRef is a powerful tool that you can learn to level up your coding skills in Microsoft Dynamics 365 Business Central. In this post, you will learn what is it and how to use it along with FieldRef and KeyRef. At the end, you will find an example of how to use them.
Before I start, I must say I first read about this on the following Kauffmann’s blog entry. So all kudos to him 🙂 When I read it I found it really interesting and used it on some of the tools we use internally. So this post will be pretty much my experience with what Kauffmann knowledge 🙂
As developers in an ever more repeatable world we should strive to write code in ways that make it as flexible and generic as we possibly can. A trick I use is too pass things around in ever more generic ways by using Variants.
A good example can be seen below where the function ReturnRecordIDasText function takes either a Record or a RecordRef as input and then returns the RECORDID as text.
In NAV 2013 R2, if a RecordRef does not have a active record, CURRENTKEYINDEX will return -1. IN NAV2009 and older, the value was 1 (As the number of the primary key).
If the value of CURRENTKEYINDEX is given to KEYINDEX – a index out of bounds error will occur.
This is not documented in the NAV 2013 R2 help.