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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

The proper way to Set Range

(0) ShareShare
ReportReport
Posted on by

Hello everybody,

I'd like to ask for a littel assistance.

I have the following C/AL code:


IF ("Sales Shipment Line".Type = Type::Item) THEN BEGIN
  gSalesLine.SETRANGE(gSalesLine."Document Type",1); 
  IF (gSalesLine.GET(gSalesLine."Document Type"::Order,"Sales Shipment Line"."Order No.","Sales Shipment Line"."Line No."))THEN BEGIN
    IF (gSalesLine.Type = Type::Item) THEN BEGIN
      gSalesLine.FINDFIRST;
    END
  END
END;

By which I'd like to retrieve the ordered quantity for a certain order from Sales Line (by accessing it from Sales Shipment Line), but for some reasons, the gSalesLine variable always gets stuck on the first record it finds (which is Quote) and for some reasons it just ignores the other SETRANGE parts... I'm not a professional NAV developer (yet), so a little help would come in handy.

Thanks in advance : Ruben 

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Mohana Yadav Profile Picture
    61,005 Super User 2025 Season 2 on at

    Did you try removing this line

    gSalesLine.SETRANGE(gSalesLine."Document Type",1);

  • Community Member Profile Picture
    on at

    Hello, thank you for Answering.

    Yes, i already tried it. the problem still persist, it always select the first record in the Sales Line table. (and the record is not modified anywhere else in any other Triggers). I tried to first Set Range on Records, which's Doc. Type is Order, and not Quote or anything else, 'thus i put that line there. but if i use the

    "gSalesLine.SETRANGE(gSalesLine."Document Type",gSalesLine."Document Type"::Order);"

    the result still will be the same.

  • Verified answer
    Mohana Yadav Profile Picture
    61,005 Super User 2025 Season 2 on at

    you should not combine SETRANGE and GET .

    You have to either use GET only or SETRANGE and FINDFIRST

    IF ("Sales Shipment Line".Type = Type::Item) THEN BEGIN

     gSalesLine.SETRANGE("Document Type",gSalesLine."Document Type"::Order);

     gSalesLine.SETRANGE("Document No.","Sales Shipment Line"."Order No.");

     gSalesLine.SETRANGE("Line No.","Sales Shipment Line"."Line No.");

     IF gSalesLine.FINDFIRST THEN BEGIN

       IF (gSalesLine.Type = Type::Item) THEN BEGIN

         MESSAGE('%1',gSalesLine."Ordered Quantity");

       END;

     END;

    END;

  • Community Member Profile Picture
    on at

    Dear Mohana,

    Thank you, it solved my issue.

    Love you, and best wishes : Ruben

  • Community Member Profile Picture
    on at

    Hi, Ruben.

    In this case, you should use GET command, because you have all the values for the Sales Line table Primary Key fields. Your code should look like this:

    IF "Sales Shipment Line".Type = Type::Item) THEN
      IF SalesLineG.GET(SalesLineG."Document Type"::Order, "Sales Shipment Line"."Order No.", "Sales Shipment  Line"."Line No.") THEN

      BEGIN

      ...Do something...
      END;

    Allow me to give you some more advice.

    Notice how this version of code is much, much simpler, elegant, readable, understandable. That is something you must always care about.

    There are no excessive round brackets "(" or ")" and BEGIN END keywords.

    Do not name your variables with prefixes like gSalesLine. Hungarian notation should not be used in NAV. Why? Well, if nothing else, simply because when you search your variables in Symbol Menu (F5), if you have many of them starting with g, for example, it won't be easy to find the exact one you need when you type "g". I guess this "g" is for "Global". You could use suffixes like G - Global, L - Local, P - Parameter and such.

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…

Neeraj Kumar – Community Spotlight

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

Leaderboard > 🔒一 Microsoft Dynamics NAV (Archived)

#1
HoangNam Profile Picture

HoangNam 7

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans