Forbidden Knowledge: How To Find Field Values from External Apps in Business Central
There’s a quote from Larry Wall, creator of the Perl programming language, that names laziness as one of the three cardinal virtues of a computer programmer. (The other two are impatience and hubris.)
Wall was half joking, and half serious. When he said “laziness,” what he actually meant was a willingness to make things efficient.
I was working on a report in Microsoft Dynamics 365 Business Central recently, and the client wanted the report to show a value from an extension that was not a dependency for my customizations app. Because I am
lazy efficient, I did not want to go to the trouble of adding an entire separate app as a dependency just to get a single field onto one report. Adding dependencies is a lot of work and requires future maintenance, and it just wasn’t worth it.
What did I do? I did some thinking, and I came up with a way to get the value I needed without having to add the dependency, that’s what. And because I am a magnanimous man of great virtue, I am sharing this with the world.
I knew I could use a RecordRef variable to find field value in the database, so long as I knew the field number. And I knew the field number because I can look it up by doing a zoom on a record in the web client. From there, all I had to do was write a codeunit to do that work for me and make sure that most of my work could be done via a Codeunit.Run() call so that, if the field didn’t exist in my database, I didn’t get an error.
Here’s my codeunit:
When you want to use it, you just need to create a RecordRef with the record from which you want to read, instantiate the codeunit, call SetParameters to tell it what to read, and then do a Run() call with error trapping to make sure it doesn’t explode on you if it fails.
For my report, I needed to read a value from the Sales Header table, so I added a function to that table that looks like this:
I set it up so that, if it didn’t find the value, it returned an empty string. If you wanted to get fancier than that, you could add more functions for individual data types to either the codeunit or the target table, but as previously mentioned, I am lazy, so you’ll have to do that on your own.
If you wanted to get fancy and write values to your outside extension, you could probably extend what I’ve written to make that work. Although if you’re doing more with an outside extension than reading a few values for a report, you should almost definitely add that extension as a dependency.
Hopefully, you’ve found something interesting and useful here. If you want a full explanation of Wall’s quote, you can find a good one in The 3 Virtues of Good Programmers.
Learn more about Business Central by viewing other How-To videos or reading more How-To blogs from ArcherPoint.
For more on Dynamics NAV and/or Business Central development, be sure to see our collection of NAV/BC Development Blogs.