When dealing with FactBoxes, it is crucial to understand the difference between OnAfterGetRecord and OnAfterGetCurrRecord trigger to avoid unexpected behaviors within Web Client
Below the links to official documentation
OnAfterGetRecord Trigger - Business Central | Microsoft Docs
OnAfterGetCurrRecord Trigger - Business Central | Microsoft Docs
The documentation related to OnAfterGetRecord trigger has been recently (March 2021) updated with a very important remark:
This trigger is independent of the currently selected record in the UI. When it gets raised depends on when the UI needs to load more data and the block size of the data read.
So, for example, in a list page, where rows are read in larger blocks, then the OnAfterGetRecord trigger will be raised for each of the records read before the page is shown.
Within the trigger, the Rec variable will refer to the record just read
Considering the statement above, there are scenarios where OnAfterGetRecord trigger execution does not necessarily update the Web Client UI hence this code should be moved into OnAfterGetCurrRecord trigger. And performance would surely benefit from this.
A more practical example.
Let’s consider
A Generic Factbox based on a temporary table to display something like
page 50130 "Generic Factbox"
{
PageType = ListPart;
ApplicationArea = All;
UsageCategory = Administration;
SourceTable = "Item";
SourceTableTemporary = true;
Editable = false;
layout
{
area(Content)
{
repeater(GroupName)
{
field(Description; Description)
{
ApplicationArea = All;
}
}
}
}
Within the Factbox, a procedure that update the value like the one below
procedure SetGenericFactboxText(Location: Record Location)
var
FirstLocation: Record Location;
TempItem2: Record "Item" temporary;
begin
Rec.Copy(TempItem2, true);
FirstLocation.FindFirst();
if Location.Code = FirstLocation.Code then begin
Rec.Init();
Rec.Description := 'Hey, I am LOCATION ' + FORMAT(Location.Code);
Rec.Insert();
end;
CurrPage.Update(false);
end;
Page extension for Location card with both triggers like below (Please note that OnAfterGetCurrRecord might or might not have any code in it. Just an explicit declaration would trigger this behavior)
trigger OnAfterGetRecord()
begin
CurrPage.Factbox.Page.SetGenericFactboxText(Rec);
end;
trigger OnAfterGetCurrRecord()
begin
end;
Deploying the extension and moving between location card record (not in the list), you might observe that the value will never ever update from the first one that is loaded.
See below a screenshot on moving from the first record to the second record (expected: a blank text)
This happens because code there is a declaration for OnAfterGetCurrRecord and code that is supposed to update UI is placed in OnAfterGetRecord.
NOTE: if you remove OnAfterGetCurrRecord trigger you might get the appropriate result but you cannot do this every time (e.g. in the base or system application or other party extensions)
To resume, then, the message to transmit and lesson learned is
- Windows Client was always updating UI, no matter if the code where placed in OnAfterGetRecord or OnAfterGetCurrRecord trigger.
- Web Client is not supposed to update the UI in this scenario using OnAfterGetRecord trigger IF there is a C/AL code in OnAfterGetCurrRecord or there is an AL OnAfterGetCurrRecord trigger declared.
The go do’s, then, is to acknowledge this behavior and in scenarios as the one proposed, move the code from OnAfterGetRecord trigger into OnAfterGetCurrRecord trigger.