The three code alternatives you provided represent different ways to retrieve and interact with data from a lookup page in a Dynamics 365 Business Central or NAV environment. The methods being compared are SetSelectionFilter, SetSelection, and GetRecord, and each behaves slightly differently in terms of handling filters, selections, and records.
codeunit 51001 "APL Item Import Handler"
{
procedure ImportAndParceExcelFile()
var
ItemImportParse: Codeunit "APL Item Import - Parse Hndlr.";
begin
ItemImportParse.ParseData();
end;
procedure ImportAndParceExcelFileWithVendorNo()
var
Vendor: Record Vendor;
ItemImportParse: Codeunit "APL Item Import - Parse Hndlr.";
VendorPage: Page "Vendor List";
begin
//Alt 1
VendorPage.LookupMode(true);
if (VendorPage.RunModal() = Action::LookupOK) then begin
VendorPage.SetSelectionFilter(Vendor);
Message('NoField: %1 - NoFilter: %2 - Count: %3', Vendor."No.", Vendor.GetFilter(Vendor."No."), Vendor.Count);
end;
Clear(VendorPage);
//Alt 2
VendorPage.LookupMode(true);
if (VendorPage.RunModal() = Action::LookupOK) then begin
VendorPage.SetSelection(Vendor);
Message('NoField: %1 - NoFilter: %2 - Count: %3', Vendor."No.", Vendor.GetFilter(Vendor."No."), Vendor.Count);
end;
Clear(VendorPage);
//Alt 3
VendorPage.LookupMode(true);
if (VendorPage.RunModal() = Action::LookupOK) then begin
VendorPage.GetRecord(Vendor);
Message('NoField: %1 - NoFilter: %2 - Count: %3', Vendor."No.", Vendor.GetFilter(Vendor."No."), Vendor.Count);
end;
end;
}
1. Alt 1 – Get Filters/Marked Selected
- Method used:
VendorPage.SetSelectionFilter(Vendor) - What it does:
- This method applies a filter to the
Vendortable based on the selected record(s) in the lookup page. It doesn’t directly select or fetch specific records but rather returns the filtered result set. - The filter is applied, and then the code retrieves the vendor based on that filter.
- The
Vendor.Countreflects the number of records that match the applied filter.
- This method applies a filter to the
- Usage:
- This approach is useful when you want to retrieve multiple selected records or apply some kind of filter for further processing. The filter is applied to the dataset, so you could retrieve several records from the lookup.
- Message Output:
- It will display the vendor number (
No.), the filter applied, and the count of records matching the filter.
- It will display the vendor number (
2. Alt 2 – SetSelection
- Method used:
VendorPage.SetSelection(Vendor) - What it does:
- This method directly retrieves the specific record(s) that have been selected on the lookup page. It transfers the selection from the page to the
Vendorvariable. - Unlike
SetSelectionFilter, this doesn’t involve applying a filter but rather retrieves the exact selected records. - The
Vendor.Countwill indicate the number of selected records, not the number of records matching a filter.
- This method directly retrieves the specific record(s) that have been selected on the lookup page. It transfers the selection from the page to the
- Usage:
- This approach is for scenarios where you want to get the actual selected records without applying any filters. It allows for multi-record selection directly from the lookup page.
- Message Output:
- It will display the vendor number (
No.), the filter (which might be empty because no filter is applied), and the count of selected records.
- It will display the vendor number (
3. Alt 3 – Get Selected Record but not filters
- Method used:
VendorPage.GetRecord(Vendor) - What it does:
- This method retrieves a single selected record from the lookup page. It doesn’t apply filters or handle multiple selections—only the current record the user selected.
- The
Vendor.Countwill typically return 1 since it fetches a single record.
- Usage:
- This is useful when you need only one selected record and do not need to deal with filters or multiple records.
- Message Output:
- It will display the vendor number (
No.), any filter that might have been applied (though usually empty), and a count of 1 because only one record is retrieved.
- It will display the vendor number (
Key Differences:
- Filters vs. Selections:
SetSelectionFilter(Alt 1) applies a filter to theVendordataset based on selected records, allowing you to handle multiple records via filtering.SetSelection(Alt 2) directly retrieves the selected records but doesn’t involve filters.GetRecord(Alt 3) retrieves just one selected record without applying any filters.
- Multi-record vs. Single Record:
SetSelectionFilter(Alt 1) andSetSelection(Alt 2) can handle multiple records, whileGetRecord(Alt 3) is for single record selection.
- When to Use:
- Use
SetSelectionFilterwhen you want to apply and work with filters based on selected records. - Use
SetSelectionwhen you want to work directly with the selected records. - Use
GetRecordwhen you only care about retrieving a single record without filters or multiple selections.
- Use
Summary:
- Alt 1: Ideal for applying filters based on multiple selected records.
- Alt 2: Retrieves all selected records directly without applying filters.
- Alt 3: Retrieves a single selected record with no filter application or multiple selection handling.
