Call Stack without Error

Have you ever been wondering how a certain value has been changed or from which part in NAV a record has been deleted. So did I and I needed to find the exact code where the value was updated or the record was deleted.
To track the table changes you can use the “Change Log” functionality, but there you can only see who has changed a value and when they did.

So, I have been searching for a why and I came past the GETERRORCALLSTACK function.
This function give me the overview of what triggers NAV has did before showing me an ERROR.
The only problem is that I don’t have an error to raise. I only have a value that is changed or a record that was deleted.

But when I’m a little bit creative, I can get my Call Stack right before the changes of a record.
Ok, what have I done.

First I have activated the “Change Log” functionality on my table and specified the field that I would like to investigate.
Then I have added a blob field in table 405 “Change Log Entry” to store the Call Stack and added it on the page with the possibility to show the content.

Next I plugged in a hook right before the insert of the “Change Log Entry” as you can see.

And finally I fill the blob field with the call stack.

To get the Call Stack I simply call an ERROR(”). The function GETLASTERRORCALLSTACK only works when an ERROR is raise. By combining it with the ASSERTERROR function the system will continue my next code and capture and saves the Call Stack.

The bad thing about this code is that I need to set a COMMIT before calling the ASSERTERROR ERROR(”) otherwise my table modification was cancelled. So I will strongly recommend you to only use this function if you really need to and definitely not in PRODUCTION !!! The risk is too big to commit part of modifications into the database and your data can get inconsistent.

I would advise you to make a setting like “Enable Call Stack” in your development environment.

After changing this value, do not forget to reopen your NAV client otherwise the new value will not be enabled. This is because all the “Change Log” triggers are called from CU1 and that codeunit is “Single Instance”. 😉

I had hoped to abandon the COMMIT from this function but until now I haven’t found a good alternative. Maybe in the future I find a better way.

Anyway the result of the code is very helpful.
When you want to know for example how the “Line Amount” is filled in on the Sales Order you can do it now.
And the result will look like this.

But if you have an import that creates a Sales Order the Call Stack will looks like this.

Ok, now you are able to trace your database changed on a different way and maybe finally find the right spot to change your code.
It can be a good idea for Microsoft to add a GETCALLSTACK function so that we don’t have to raise a fake error to get the Call Stack. We shall see what the future will bring us.

And don’t forget DON’T use it in PRODUCTION environments.

2 thoughts on “Call Stack without Error

  1. Punky says:

    You can also raise a fake error the old fasion way by calling IF CODEUNIT.RUN(…) THEN; And call the error in the codeunit.
    But reminded me the new ASSERTERROR function, which is normally used in Test Codeunits.

Leave a Reply

Your email address will not be published. Required fields are marked *