With BC 2020 wave 2 a new feature was introduced that allows for background checks on journal lines.
See more about this new feature here.
“On the General Journal Batch page, you can choose Background Error Check to have Business Central validate financial journals, such as general or payment journals, while you’re working on them. When the validation is enabled, the Journal Check FactBox displays next to the journal lines and will show issues in the current line and the whole batch. Validation happens when you load a financial journal batch, and when you choose another journal line”.
Let’s see how that works.
From the General Journal page, lookup into Gen. Journal Batches:
Enable “Background Error Check”.
In the Default general journal batch we can see now a new factbox : “Journal Check”:
We can observe that while we edit the journal the background check takes place.
If we click on the 3rd cue, “Issues Total”, we can see the errors:
We see that the Amount on the first line is 0.
Let’s update it to “12”:
We can now see that the error “Amount must not be empty” is gone, but we still have the error: “Document No. … is out of balance”.
Let’s update one of the lines so that the sum of all lines is 0.
After we update the Amount on the first line with -11 the errors are gone:
How is this checking in the background working?
With BC 2019 wave 2 release introduces a way for AL developers to start programming using multithreading/asynchronous concepts.
Developers can now calculate expensive operations in a page without blocking the UI, and then update the UI once the calculation is complete.
Read Microsoft document on background tasks here.
If we were to look at page 1921 “Journal Errors Factbox”, in the OnAfterGetCurrRecord()
The method CheckErrorsInBackground() contains a line that enqueues the codeunit responsible with the general journal lines checking:
The check is done in the codeunit 9081 “Check Gen. Jnl. Line. Backgr.” in the OnRun() method:
Digging deeper, ultimately the standard codeunit 11 “Gen. Jnl.-Check Line” is run for each line.
For each journal line, errors are collected and made available for Counts to the factbox:
For example, for the second cue, “Lines with Issues” the system uses the factbox method GetNumberOfLinesWithErrors();
What about custom validations?
How can we catch those?
In a table extension I added a text field:
Exposed it on the page via a page extension:
At last, in a codeunit, I subscribed to an event from codeunit 11 “Gen. Jnl.-Check Line”
And, if we remove the value of “My test field” in one or more of the lines we can see the TestField error captured by the background task:
For more details, including the implementation of a completely new page background task check Tobias Fenster article.