One of the news in AL Language that is coming with the upcoming major version (Business Central 2021 W1) in April is a new trigger AnAfterLookup. The new trigger is described on Microsoft Docs (however, the description is still a bit confusing…).
As the Lookups are often used, I already tried what’s new and how we can improve our projects with these changes. Unfortunately, there is no documentation available at the time of this article, so I am unsure what is intended and what is currently just a bug (as this is still only a part of the preview version).
Although I could use the triggers in some small examples, I still can not find any scenario where the OnAfterLookup trigger can be useful in the real world. Let’s look at some examples.
OnLookup & OnAfterLookup
The OnLookup trigger is known from the beginning of C/AL Language. It is used to build more complex Lookups that can not be created using TableRelation (or any similar property). It replaces standard lookup features (runs in a place of regular lookup feature).
Until the Roletailer Client (NAV2009RT or rather NAV 2013), there was no visual/functional difference between lookups created through any property or OnLookup trigger. However, with Roletailored client (and Web Client), there is a vast difference – a dropdown list available only for relations made through TableRelation property. The dropdown list is beneficial for users, and developers must have it in mind when creating relations.
Examples
OnAfterLookup trigger does not override the standard trigger from the TableRelation. It is triggered once the record is selected from the Dropdown box; however, if the user decides to use a standard lookup list using “Select from a full list”, the trigger is not fired.
With this in mind, it is unsurprising that both triggers written for the same field are useless (OnAfterLookup is not used as OnLookup does not use dropdowns). In the first example below, the OnAfterLookup trigger is never called.
field("TKA Tariff No."; Rec."TKA Tariff No.")
{
trigger OnLookup(var Text: Text): Boolean
begin
Message('Old Value: ' + Text);
Text := YourOwnLookupReturningSelectedRecord()
Message('New Value: ' + Text);
exit(true);
end;
trigger OnAfterLookup(Selected: RecordRef)
begin
Message('Selected Value: ' + Format(Selected));
end;
}
If we only use the OnAfterLookup trigger, the trigger is fired once the user chooses the DropDown list value. As a developer, you get a RecordRef variable that contains the selected record.
The second example shows how you can print information about the selected record. However, I am still unsure how to use it in the real world – we can just get information from the selected record (and even only for those made through DropDown List, not the full list).
field("TKA Tariff No."; Rec."TKA Tariff No.")
{
trigger OnAfterLookup(Selected: RecordRef)
var
TariffNumber: Record "Tariff Number";
begin
Selected.SetTable(TariffNumber);
Message('Selected value: ' + TariffNumber."No.");
end;
}
Do you have any ideas on how to use this functionality within real projects? Have you ever used the Text parameter in the OnLookup trigger? Please share your thoughts in the comment section.