Use of FINDFIRST FINDLAST and FINDSET in BC D365

Use of FINDFIRST FINDLAST and FINDSET in BC D365:

(1) For finding records in tables according to the filters and current key on tables in Business Central, use FINDFIRST, FINDLAST and FINDSET.

(2) FINDFIRST is defined as finding the first record in the table according to the filter and current key.

(3) FINDLAST is defined as finding the last record in the table according to the filter and current key.

(4) FINDSET is defined as finding a set of records in the table according to the filter and current key.

(5) The difference between FINDFIRST, FINDLAST and FINDSET is that, if you find the first or last record then use FINDFIRST and FINDLAST function and if you find a number of particulars records then use the FINDSET function.

(6) Note- FINDFIRST and FINDLAST: only use these functions when you want to find the first & last record in a table or set. Do not use this function in combination with REPEAT… UNTIL.

(7) Note- FINDSET: only uses this function only when you explicitly want to loop through a record set. You should only use this function in combination with REPEAT… UNTIL.

(8) Syntax of FINDFIRST is:

[Ok :=] Record.FINDFIRST

(9) Syntax of FINDLAST is:

[Ok :=] Record.FINDLAST

(10) Syntax of FINDSET is:

[Ok :=] Record.FINDSET([ForUpdate][, UpdateKey])

* By default “ForUpdate” and “UpdateKey” values are false. If it is true, then the system auto call “Locktable” function in the logic before reading the data in the table.

(11) Let’s take an example, find value in the Sales Header table by using FINDFIRST, FINFLAST and FINDSET as shown.

FINDFIRST FINDLAST and FINDSET in BC D365

Source Table:

codeunit 50001 “Custom Functions-01”
{
    trigger OnRun()
    begin
 
    end;
 
    local procedure HowToApplyFiltersOnTableinBCD365()
    Var
        ApplyFilteronSalesHeader: Record “Sales Header”;
    begin
        //Case-01 Use of FINDFIRST
        ApplyFilteronSalesHeader.reset; //Removing intial filters on table
        if ApplyFilteronSalesHeader.FindFirst() then //Find FIRST record in table

message(‘First Sales Order No.: %1’, ApplyFilteronSalesHeader.”No.”);
 
        //Case-01 Use of FINDLAST
        ApplyFilteronSalesHeader.reset; //Removing intial filters on table
        if ApplyFilteronSalesHeader.Findlast() then //Find LAST record in table
            message(‘Last Sales Order No.: %1’, ApplyFilteronSalesHeader.”No.”);
 
        //Case-01 Use of FINDSET
        ApplyFilteronSalesHeader.reset; //Removing intial filters on table
        if ApplyFilteronSalesHeader.FindSET(True,True) then begin //Find Set of record in table
            repeat
                message(‘Sales Order No.: %1’, ApplyFilteronSalesHeader.”No.”);//Show all SO No. one by one
            until ApplyFilteronSalesHeader.next = 0;
        end;
    end;
}

Use of SETRANGE and SETFILTER in Dynamics 365
How to use CASE statement in D365 BC
How to use FOR-TO loop statement in Microsoft D365
How to add or show new field in existing Page in D365 BC
How to configure GST Setup in Dynamics 365 BC
FINDSET Function (Record)– Microsoft Docs

Leave a Reply