How checking financial journal in background works
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.
This was originally posted here.

Like
Report
*This post is locked for comments