Be careful making JSON types equal to one another. When you do that you copy the reference, not the value. This caught me out.
I’m implementing an interface which accepts a JsonObject parameter expecting that you will assign a value which will be used later on. The interface doesn’t require that the JsonObject is passed with var. In fact, it requires that it isn’t. If you include var the compiler will complain that you haven’t implemented all of the interface methods. Something like the JsonExample action in the below code.
“That’s never going to work, the parameter needs to be passed with var” I thought. Better still, just have method return a JsonObject type. However, the interface probably pre-dates complex return types so we’ll let that go. Although, I think you could still return JSON types even before complex return types were introduced…but let it go.
I was surprised that it did work. Call JsonExample and you get:
That’s because even without the var keyword the JsonObject variable holds a refence to the object rather than the value itself, so it still exists after CalcJson() has finished executing.
OK, great. I went on to create a separate codeunit to handle the creation of the JsonObject. I wanted to add some error handling and separate the boilerplate of the interface implementation from the business logic.
I wrote something like CalcJson2(). My tests started failing. It seemed that the JsonObject was empty. That puzzled me for a while. What had I done wrong? I think this is the problem.
The JsonObject referenced by the Result variable in codeunit 50101 is created and has the properties added
This reference goes out of scope once CalcJson has finished executing and its value is lost/garbage collected/however it works in Business Central
The JsonObject referenced by the Object parameter is made equal to the first i.e. now points to the first JsonObject in memory – but that value has already gone
As the result the second JsonObject is empty when it is handed back to the calling code
Instead of making the JSON types equal to one another explicitly copy the value of one to the other. Like this:
procedure CalcJson3(Object: JsonObject)
CalcJson: Codeunit "Calc. Json";
In this case writing the value of one to text and then reading it back in to the other. It looks a bit weird, but it works. JsonObject also has a Clonemethod.
I do this fairly often to prep a new (local) container for development. The script needs to be run on the Docker host and assumes that the PowerShell prompt is already at the folder containing the apps (or apps in child folders) you want to install.
Use the dev endpoint if you are going to want to publish the same apps from VS Code into the container. Or publish into the global scope if you prefer – maybe if you are creating an environment for testing.
codeunit 50104 "Get Callstack"
SingleInstance = true;
procedure GetCallstack() Callstack: Text
LF := 10;
Callstack := GetCollectedErrors(true).Get(1).Callstack;
exit(Callstack.Substring(Callstack.IndexOf(Format(LF)) + 1));
I dunno, I was just curious whether it was possible. And, it is 🧐 Any sensible applications are probably going to be do with error handling or reporting.
You may be tempted to have your code respond differently depending on the context in which it has been called and read the callstack for that purpose. That’s not a train you want to ride though. I’ve tried, it stops at some pretty weird stations.
One advantage of this approach over using a TryFunction (as below) is that the debugger doesn’t break on collectible errors. It can sometimes be frustrating stepping through errors that are always caught to get to the code that you actually want to debug.
procedure LessGoodGetCallstack(): Text
In part 3 we had a look at the new platform feature, collectible errors. Long story short: it makes scenarios where you want to collect and display multiple errors together (e.g. checking journal lines) much easier to code and read, no messing around with if Codeunit.Run then, read the post if you’re interested.
The only thing that let the team down was the horrible user interface to display the errors that have been collected. Shame, but at least the API has been designed with extensibility in mind and we can handle it ourselves. I’m using the same example as in the docs (but with a little added finesse).
There is a new global method, GetCollectedErrors. This will return a list containing the ErrorInfo objects which have been collected in the current transaction. That gives us all the detail that we need to display the errors in a user-friendlier way.
Show Errors Codeunit
I’ve created a new codeunit to accept the list of errors and show the standard Error Messages page. Of course, you can do whatever you want with this. Create a new table and page to display the errors if you like. For now, the Error Messages page does everything that I need.
codeunit 50103 "Show Errors"
SingleInstance = true;
procedure ShowErrors(Errors: List of [ErrorInfo]; Context: Variant)
TempErrorMsg: Record "Error Message" temporary;
DataTypeMgt: Codeunit "Data Type Management";
if Errors.Count() = 0 then
foreach ErrorInfo in Errors do begin
TempErrorMsg.LogDetailedMessage(ErrorInfo.RecordId, ErrorInfo.FieldNo, TempErrorMsg."Message Type"::Error, ErrorInfo.Message, ErrorInfo.DetailedMessage, '');
if DataTypeMgt.GetRecordRef(Context, RecRef) then
TempErrorMsg.Validate("Context Record ID", RecRef.RecordId());
Page.Run(Page::"Error Messages", TempErrorMsg);
A couple of notes on the above:
There is a SetContext method on the error message record which I’m not using. For reasons I’m not clear about that method sets the context fields on a global variable in the Error Message table, not on the record instance itself
Calling the Log methods on the Error Messages table captures the current callstack*, I’m overwriting that with the callstack on the Error Info object
The Error(”); on the final line ensures that code execution is stopped and the transaction rolled back. An error like this (outside of If Codeunit.Run then) cannot be collected
I’ve added the last line to the CheckLines method (the method which is collecting the errors):
local procedure CheckLines(VideoCallBatch: Record "Video Call Batch")
VideoJnlLine: Record "Video Journal Line";
ShowErrors: Codeunit "Show Errors";
//check lines code
The result is something like this. The Error Messages page does the rest, drilling down to show the callstack, adding an action to open the related record, displaying the name of the field name related to the error.
Check the docs for more info. The error info API includes IsCollectingErrors(), HasCollectedErrors() and ClearCollectedErrors().