web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested answer

Find last second record in the table

(0) ShareShare
ReportReport
Posted on by 150

Hello Expert,

I am trying to find last second record of the table but my program is not working. Can anyone please help.

Count1:=0;
IF Cust.FIND('+') THEN
REPEAT
Count1:= Count1 +1;
UNTIL Cust.NEXT= -1;

Thanks in advance.

Nia

I have the same question (0)
  • Suggested answer
    Tabrez Ajaz Profile Picture
    785 on at
    RE: Find last second record in the table

    Hi Nia,

    The following code will work perfectly whenever you want to find the second last record and no looping required at all. When you use my code you have to use at least 2 variables of record type for the same table. {example using the customer table}

    trigger OnAction()
    var
        recCust: Record Customer;
        recCust1: Record Customer;
    begin
        recCust.FindLast();
        recCust1.SetFilter("No.", '<>%1', recCust."No.");
        if recCust1.FindLast() then
            Message('Customer No: ' + Format(recCust1."No.") + ', Name: ' + recCust1.Name);
    end;
     
    The above code will work, to get second the last customer you can also achieve this by using 1 record type and another code type of variable to store the customer primary key ("No.") field value to filter the Customer table for the second time.
    trigger OnAction()
    var
        recCust: Record Customer;
        CustomerNo: Code[20];
    begin
        recCust.FindLast();
        CustomerNo := recCust."No.";
        recCust.SetFilter("No.", '<>%1', CustomerNo);
        if recCust.FindLast() then
            Message('Customer No: ' + Format(recCust."No.") + ', Name: ' + recCust.Name);
    end;
    I hope you understand now, how we can achieve this without iterating the records, attaching some screenshots to confirm you this is working:
    pastedimage1624465257293v1.png  pastedimage1624465280918v2.png
  • Suggested answer
    Suresh Kulla Profile Picture
    50,080 Super User 2025 Season 2 on at
    RE: Find last second record in the table

    Nia, it will not just exit the look, you need to add the condition like this or when you get your record assign a boolean to true and add that after the next condition..

    i := 0

    IF Cust.FINDLAST THEN

    REPEAT

      i += 1;

      IF i =2 THEN BEGIN

      END;

    UNTIL (Cust.NEXT(-1) = 0) or (i =2);

  • Nia Profile Picture
    150 on at
    RE: Find last second record in the table

    Hi Suresh,

    i tried your code and even i had almost same code but result runs in loop. I want to show only single record and then the function should exit.

    MESSAGE(Cust."No.");

    Count1:=0;

    IF Cust.FIND('+') THEN

    Cust.NEXT:= -1;

    i tried above and could achieve the result but it shows the record multiple times.

  • Suggested answer
    Suresh Kulla Profile Picture
    50,080 Super User 2025 Season 2 on at
    RE: Find last second record in the table

    You can loop backwards using the below record or you could set the SETASCENDING to false on the key and loop

    IF Cust.FINDLAST THEN

     REPEAT

     UNTIL Cust.NEXT(-1) = 0;

  • Samantha73 Profile Picture
    3,174 on at
    RE: Find last second record in the table

    Why not just use FINDLAST

    dynamicsuser.net/.../query-on-find-findset-findfirst-findlast

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
Nimsara Jayathilaka. Profile Picture

Nimsara Jayathilaka. 3,885

#2
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 3,405

#3
Sumit Singh Profile Picture

Sumit Singh 2,837

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans