D365 Business Central : Page OnLookup Trigger

There are multiple ways in BC to create a Lookup button for a variable in a page or report. Generally speaking, I have seen three following methods being done to achieve the lookup button.

1. Using Table Relation

field(fieldItemCode1; ItemCode)
{
    ApplicationArea = All;
    Caption = 'Item Code 1';
    TableRelation = Item."No.";
    trigger OnValidate()
    begin
        Message(ItemCode);
    end;
}

This is the method that works best. The validation is triggered and you also get the native drop-down list which is nice.
However, there is limitation on filtering, so you will not be able to use this method every time.

Native Lookup

2. Using OnLookup trigger and assigning the code directly to the variable.

field(fieldItemCode2; ItemCode)
{
    ApplicationArea = All;
    Caption = 'Item Code 2';

    trigger OnLookup(var Text: Text): Boolean
    begin
        If Page.RunModal(Page::"Item Lookup", Item) = Action::LookupOK then
            ItemCode := Item."No.";
    end;

    trigger OnValidate()
    begin
        Message(ItemCode);
    end;

}

This method is also common, but have issue on related triggers. As you can see, native trigger, such as OnValidate is not triggered when you do it this way.

No Message Validation trigger

3. Using OnLookup trigger and assigning the code to the Text and exit TRUE.

field(fieldItemCode3; ItemCode)
{
    ApplicationArea = All;
    Caption = 'Item Code 3';

    trigger OnLookup(var Text: Text): Boolean
    begin
        If Page.RunModal(Page::"Item Lookup", Item) = Action::LookupOK then begin
            Text := Item."No.";
            exit(true);
        end;
    end;

    trigger OnValidate()
    begin
        Message(ItemCode);
    end;
}

This method give you the lookup function, but also trigger the validation when you call exit(true). The downside is that it does not give you dropdown list on method 1 (using Table Relation).

Message Validation

I would recommend to use method 1 as much as possible. Only when it is not possible, we should be using method 3.

thatnavguy

Experienced NZ-based NAV Developer and Consultant with 15+ years of experience leading multiple IT projects, performing business analyst, developing, implementing, and upgrading Dynamics NAV and Business Central. Passionate to deliver solution that focuses on user-friendly interface while keeping high standard of compliance with the needs.

You may also like...

1 Response

  1. 23 September 2021

    […] Page OnLookup Trigger […]

Leave a Reply

Your email address will not be published. Required fields are marked *