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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

Understanding queries (setfilter, find, etc.)

(0) ShareShare
ReportReport
Posted on by 487

Hello Everyone,

I have reread the documentation a couple of times, and I still have a hard time to grasp how the queries work.

For example, if i want to delete a record, here is my result in c/al, compared to a simple sql statement:

Var

lines : Record "Sales Line";

Begin

lines.Init();
lines.SETFILTER("Document No.", "No.");
lines.SetRange("Line No.", 2);
lines.Find('-');
lines.Delete();

The sql version is

DELETE FROM "Sales line" WHERE "Document No." = "No." AND "Line No." = 2;

Am I missing something? Or is it really that convoluted?

Cheers

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at

    You can lose the init line. INIT is used to initialise a record variable and set all fields (except primary key fields) to either blank values or their predefined InitValue. Usually you do this when creating a new record.

    Also you should add (TRUE) as a parameter to the end of your DELETE statement. This invokes the OnDelete trigger of the table, and is important as code there checks the validity of the action, and also deletes connected records (eg comments, reservations).

    If this is specifically for table 37 Sales Line, you should also filter Document Type, as there can be multiples of the same Document No., eg an Order and an Invoice.

    Shorter code would simply be:

    SalesLine.GET("Document Type","No.",2);
    SalesLine.DELETE(TRUE);

    But to simply fix up your code a bit:

    lines.SETRANGE("Document Type","Document Type");
    lines.SETRANGE("Document No.","No.");
    lines.SETRANGE("Line No.", 2);
    lines.FINDFIRST;
    lines.DELETE(TRUE);

  • Hedi Daneels Profile Picture
    487 on at

    The shorter version already looks way better than mine. Thank you. I understand a couple of things better now!

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,850 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,084 Super User 2026 Season 1

#3
Dhiren Nagar Profile Picture

Dhiren Nagar 959 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans