An exploding chain

How to avoid a dependency

With the release of Business Central, Microsoft introduced the concept of Extensions. An Extension is a secluded function to the standard functions of Business Central. It’s a great concept – but it comes with an annoying flaw. Read in this article how you can avoid to add a dependency to your Extension.

To be honest, I was thinking about this topic for a while now but I didn’t see a real purpose in writing a blog about it. I thought, with the release of Business Central version 16 we would get rid of the problem finally. But, as it turned out, there is a big problem: No guarantee that all Extensions with a dependency will be published and installed again. I was not able to get to the bottom of it, but when publishing from VSCode, the process “forgets” some Extensions. This is critical and needs attention from every developer.

Another reason why I decided to write this blog though, is the circumstance that it is not possible in all kind of environments to publish from VSCode. Production-environments tend to have a different setup than a development-environment and do not allow the direct publish from VSCode. That means, that developers have to go into PowerShell to unpublish/uninstall/publish/install Extensions manually.

My old friends, RecordRef and FieldRef

Now, I don’t want to beat around the bush more than necessary. Please review the code below.

Example code how to use RecordRef & FieldRef to avoid a dependency.
Example code how to use RecordRef & FieldRef to avoid a dependency.

As you can see, the basic concept is to utilize RecordRef and FieldRef. Both Data Types reference records and field values during runtime. This trick allows to retrieve values from Extension-Tables & -Fields without adding a Dependency in the app.json. A similar concept can be used to write data also (including field validation). The only disadvantage of this concept is the missing compability check. When applying such code, it is in the responsibility of the developer that the requested Table & Field is installed. It is also necessary to ensure that the right Data Type is provided when writing data as demonstrated in below example.

Example code how to use RecordRef & FieldRef to write data without a dependency.
Example code how to use RecordRef & FieldRef to write data without a dependency.

Verdict

This blog will not feed you every detail how to apply this concept and simply copy & paste. From my personal experience I can say, that I did extend this concept widely in my projects to read and write data into different Tables and fields. I even use it to execute a function in a codeunit without a Dependency. So, when you think this might make things easier for your projects – please create your own functions!

It is important to consider, that in a situation in which the rules of the system are avoided, the comfort that a system provides is lost! The code must be robust enough to handle situations in which a field or record doesn’t exists. In case a problem would appear, it would be reported as a runtime error.

But, do the advantages outweight the disadvantages? For me, in some situations, yes.

Leave a Comment

Scroll to Top
%d bloggers like this: