Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Unanswered

OnAfterGetCurrRecord vs OnAfterGetRecord vs FactBoxes UI update

(0) ShareShare
ReportReport
Posted on by

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)

 

 pastedimage1615479356939v1.png

 

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

  1. Windows Client was always updating UI, no matter if the code where placed in OnAfterGetRecord or OnAfterGetCurrRecord trigger.
  2. 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.

  • Andy Sather Profile Picture
    Andy Sather on at
    RE: OnAfterGetCurrRecord vs OnAfterGetRecord vs FactBoxes UI update

    Thank you Duilio

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,642 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,371 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans