Dynamics 365 Business Central: ClearAll is evil, again!

When Dynamics 365 Business Central version 16 was launched, I remember that a weird problem was signaled to Microsoft related to the ClearAll usage in code. For who don’t know this (but who don’t know this?) the ClearAll() procedure clears all internal variables (except REC variables), keys, and filters in the object and in any associated objects, such as reports, pages, codeunits, and so on that contain AL code. The weird problem (officially fixed by Microsoft on v16.X) was related to clearing also variables declared in other extension’s objects.

Now we’re on Dynamics 365 Business Central version 17.4 and v18 is still in the horizon (you will be able to create preview sandboxes with v18 in a week or so). When working with a complex partner’s addon in these days, imagine my surprise when discovering that the ClearAll evil is back again!

To explain the problem, imagine that your solution creates a page (here called MyItemList) and this page performs a ClearAll() in the OnAfterGetRecord trigger and then executes your custom code:

Now you (or another partner) creates a new extension that depends from this main extension and on this new app you have a business process defined:

Then, you create a pageextension object of the base page (MyItemList) and on this pageextension object you execute a custom code like the following:

Here, in the OnOpenPage trigger you execute a procedure (here called PerformCheck, for simplicity I’ve defined it in the pageextension itself, but please don’t do that :)). If this procedure returns true, in the OnAfterGetCurrRecord trigger you execute the Process1 procedure, otherwise you execute the Process2 procedure (totally different business process).

What do you expect by checking the above code? That if I open the MyItemList page, Process1 is executed. You deploy the app and what happens?

The ClearAll() procedure clears all the internal variables, also variables declared in the extension’s objects!!

The bad things is that also Microsoft’ Base Application code is affected by this problem, because there are standard pages (like page 400 “Purchase Invoice Statistics” for example) that does a terrible ClearAll in the OnOpenPage trigger too:

Unfortunately, this happens today on version 17.4 (current release) and also on the v18 private previews. Microsoft is alerted about that, I hope on a fix before the v18 general availability, but you should be alerted too. If you’re using ClearAll() in your code today, please check if it’s strictly needed of if you can do better things (personally I hate using this procedure and I would like to have it deprecated). This advice is just to avoid to have strange behaviours on your apps.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.