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.

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.

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).

I would recommend to use method 1 as much as possible. Only when it is not possible, we should be using method 3.
1 Response
[…] Page OnLookup Trigger […]