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

Notifications

Announcements

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

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

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

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,703

#2
YUN ZHU Profile Picture

YUN ZHU 803 Super User 2025 Season 2

#3
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 750 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans