OnAfterLookup: New page field trigger coming in v18

by Feb 28, 2021AL Language

Home 9 Development 9 AL Language 9 OnAfterLookup: New page field trigger coming in v18

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.

Recent Articles from the category

BC Open Source? How to start?

BC Open Source? How to start?

BC Open Source? How to start? One of the most exciting news introduced last month in Lyon during Directions EMEA 2023 was the changes to the open-source initiative. This means that you can now contribute to the source code of the Base app and the System app, which are...

read more
Validate a FlowField Field. Wait? What?

Validate a FlowField Field. Wait? What?

Validate a FlowField Field. Wait? What? There are not many things in the AL Language that surprised me. However, last week, I found one such thing - I reviewed customizations made by another partner and had to analyze the OOTB code of the Demand Forecast matrix. I run...

read more
Dynamics NAV 2013 & Expired Cronus License

Dynamics NAV 2013 & Expired Cronus License

We found an interesting problem - we were not able to run the development environment for Dynamics NAV 2013. Whenever we tried to run the development client, we got the following error message: "Your program license has expired" and the development client has closed...

read more
Indirect Dependencies and Access Modifiers

Indirect Dependencies and Access Modifiers

Last week, there was a discussion on Yammer on how to get values from the "Sent Email" record when all fields are marked as Internal. I was surprised that many people do not know what can/can't access modifiers (such as local, protected, or internal) be used for. I...

read more
AL Extensions: Replace Document Attachment

AL Extensions: Replace Document Attachment

I have published a new simple, open-source extension that allows replacing existing document attachments in all master entities as well as in open documents. The source code as well as the app file that can be installed in your environment is available on my GitHub...

read more

Sign Up for News

Certifications

Highest certification
Microsoft Data Management and
also in D365 Business Central

Microsoft Certified: Dynamics 365 Business Central Functional Consultant Associate

See other certifications here