SetSelectionFilter vs SetSelection vs GetRecord

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 Vendor table 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.Count reflects the number of records that match the applied filter.
  • 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.

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 Vendor variable.
    • Unlike SetSelectionFilter, this doesn’t involve applying a filter but rather retrieves the exact selected records.
    • The Vendor.Count will indicate the number of selected records, not the number of records matching a filter.
  • 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.

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.Count will 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.

Key Differences:

  • Filters vs. Selections:
    • SetSelectionFilter (Alt 1) applies a filter to the Vendor dataset 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) and SetSelection (Alt 2) can handle multiple records, while GetRecord (Alt 3) is for single record selection.
  • When to Use:
    • Use SetSelectionFilter when you want to apply and work with filters based on selected records.
    • Use SetSelection when you want to work directly with the selected records.
    • Use GetRecord when you only care about retrieving a single record without filters or multiple selections.

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.
Share the Post:

Related Posts