Use of SETRANGE and SETFILTER in Dynamics 365

SETRANGE and SETFILTER in Dynamics 365:

(1) For applying filters on tables in Business Central, use “SETRANGE” and “SETFILTER”.

(2) SETRANGE is defined as applying a simple filter on a table.

(3) SETFILTER is defined as applying a filter that you specify.

(4) The difference between SETRANGE and SETFILTER is that in SETFILTER, you can use expression while applying a filter on table field (<, >, *, &, |, and =).

(5) Syntax of SETRANGE is-

Record.SETRANGE(Field [,FromValue] [,ToValue])

(6) Syntax of SETFILTER is-

Record.SETFILTER(Field, String, [Value],…)

(7) Let’s take an example, find value in the Sales Header table by using SETRANGE and SETFILTER as shown.

Example-01: In this example, one value filter is applied on the sales header table-

one value filter is applied on sales header table in business central

Source Code:

codeunit 50001 “Custom Functions-01”
{
    trigger OnRun()
    begin
 
    end;
 
    local procedure HowToApplyFiltersOnTableinBCD365(DocumentNo :Code[20])
    Var
        ApplyFilteronSalesHeader: Record “Sales Header”;
    begin
        //Case-01 Use of Setrange
        ApplyFilteronSalesHeader.reset; //Removing intial filters on table
        ApplyFilteronSalesHeader.setrange(“No.”,DocumentNo); //Apply filter on table
        if ApplyFilteronSalesHeader.FindFirst() then //Find filter value in table
            message(‘Sales Order exist’);
 
        //Case-02 Use of Setfilter  
        ApplyFilteronSalesHeader.reset; //Removing intial filters on table
        ApplyFilteronSalesHeader.setfilter(“No.”, ‘=%1’, DocumentNo); //Apply filter on table using expression
        if ApplyFilteronSalesHeader.FindFirst() then //Find filter value in table
            message(‘Sales Order exist’);
    end;
}

Example-02: In this example, multiple values with condition filter is applied-

multiple values with condition filter is applied in business central

Source Code:

codeunit 50001 “Custom Functions-01”
{
    trigger OnRun()
    begin
 
    end;
 
    local procedure HowToApplyFiltersOnTableinBCD365()
    Var
        ApplyFilteronSalesHeader: Record “Sales Header”;
    begin
        //Case-01 Use of Setrange
        ApplyFilteronSalesHeader.reset; //Removing intial filters on table
        ApplyFilteronSalesHeader.setrange(“No.”, ‘12000’, ‘14000’); //Apply filter on table
        if ApplyFilteronSalesHeader.FindFirst() then //Find filter value in table
            message(‘Sales Order exist’);
 
        //Case-02 Use of Setfilter  
        ApplyFilteronSalesHeader.reset; //Removing intial filters on table
        ApplyFilteronSalesHeader.setfilter(“No.”, ‘<>%1’, ‘12000’); //Apply filter on table using expression
        if ApplyFilteronSalesHeader.FindFirst() then //Find filter value in table
            message(‘Sales Order exist’);
    end;
}

How to Show Image or Picture in Report in D365 BC from Table Field
How to change Caption or Name of existing Field in D365 BC
How to add field in existing Table using extension In D365 BC
How to Attach MDF file Without LDF File by using SSMS
Getting Started with AL– Microsoft Docs

Leave a Reply