Skip to main content

Notifications

Page OnLookup Trigger

TeddyH Profile Picture TeddyH 12,868 Super User 2024 Season 1

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.


This was originally posted here.

Comments

*This post is locked for comments