Explaining Setrange

JamboJambo Member Posts: 12
edited 2008-06-16 in Navision Attain
Hi,

can somebody explain me what the following code line means:
SalesOrderLine.SETRANGE("No.","No.");

While the meaning of the first "No." is clear to me,
the second one is confusing me. The second one should be
a value or not?

Answers

  • DenSterDenSter Member Posts: 8,307
    That is a way of linking one record variable to another one. The second "No." refers to the "No." field of that other variable. It is probably part of a WITH block, or part of some dataitem code, or in code the "No." field got set and then it wants to filter on that value.
  • JamboJambo Member Posts: 12
    Daniel,

    thanks for prompt answer.
    It is probably part of a WITH block, or part of some dataitem code, or in code the "No." field got set and then it wants to filter on that value.

    I have found this type of code mostly in OnValidate triggers in serveral
    Tables. There are no WITH block or dataitem nor "No." got set in code.
    You can view it for example in Table Post Code (225) in OnValidate
    trigger. Let me know your opinion.
  • DenSterDenSter Member Posts: 8,307
    OK, so taking that table as an example. In the Code - OnValidate trigger it says this:
    PostCode.SETCURRENTKEY("Search City");
    PostCode.SETRANGE("Search City","Search City");
    PostCode.SETRANGE(Code,Code);
    IF PostCode.FIND('-') THEN
      ERROR(Text000,FIELDCAPTION(Code),Code);
    
    Now remember we are in the "Post Code" table, so every field reference is to the table itself. this means when you see "Search City" without a record variable reference, that means it is referring to the table's field value.

    Alright, assuming you're still with me... let's take the second lline apart:
    PostCode.SETRANGE("Search City","Search City");
    
    • PostCode.SETRANGE means we are going to set a range filter on the PostCode variable, which is a record type variable pointing to the Post Code table.
    • PostCode.SETRANGE("Search City" means we are going to apply the filter to the "Search City" field of the PostCode variable
    • The second "Search City" specifies the value of the filter, and refers to the "Search City" field of the current Post Code record.
  • kinekine Member Posts: 12,562
    Jambo wrote:
    Daniel,

    thanks for prompt answer.
    It is probably part of a WITH block, or part of some dataitem code, or in code the "No." field got set and then it wants to filter on that value.

    I have found this type of code mostly in OnValidate triggers in serveral
    Tables. There are no WITH block or dataitem nor "No." got set in code.
    You can view it for example in Table Post Code (225) in OnValidate
    trigger. Let me know your opinion.

    Do not forget that there is implicit "WITH Rec do" in all objects...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • ReinhardReinhard Member Posts: 249
    PostCode.SETRANGE("Search City","Search City"); 
    PostCode.SETRANGE(Code,Code);
    

    is a shorter way of doing:
    PostCode.SETRANGE("Search City",PostCode."Search City"); 
    PostCode.SETRANGE(Code,PostCode.Code);
    

    because like Denster says
    are in the "Post Code" table, so every field reference is to the table itself

    Specifically in this situation you are filtering the entire record set PostCode on a field by the value in the current record.
  • kinekine Member Posts: 12,562
    Reinhard wrote:
    PostCode.SETRANGE("Search City","Search City"); 
    PostCode.SETRANGE(Code,Code);
    

    is a shorter way of doing:
    PostCode.SETRANGE("Search City",PostCode."Search City"); 
    PostCode.SETRANGE(Code,PostCode.Code);
    

    because like Denster says
    are in the "Post Code" table, so every field reference is to the table itself

    Specifically in this situation you are filtering the entire record set PostCode on a field by the value in the current record.

    This is not true until there is somewhere With PostCode Do...

    Else it is in real:
    PostCode.SETRANGE("Search City",Rec."Search City"); 
    PostCode.SETRANGE(Code,Rec.Code);
    
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • DenSterDenSter Member Posts: 8,307
    kine wrote:
    Else it is in real:
    PostCode.SETRANGE("Search City",Rec."Search City"); 
    PostCode.SETRANGE(Code,Rec.Code);
    
    PostCode points at the record variable
    Rec points back at the table you are in. This is left out in the first example.
    Another possibility is when you are in a report dataitem for the Post Code table, called "Post Code":
    PostCode.SETRANGE("Search City","Post Code"."Search City"); 
    PostCode.SETRANGE(Code,"Post Code".Code);
    
    Note the difference between PostCode (no space, no "") and "Post Code" ("" and space):
    PostCode points at the PostCode variable
    "Post Code" points back at the dataitem.
  • DenSterDenSter Member Posts: 8,307
    Reinhard wrote:
    PostCode.SETRANGE("Search City","Search City"); 
    PostCode.SETRANGE(Code,Code);
    

    is a shorter way of doing:
    PostCode.SETRANGE("Search City",PostCode."Search City"); 
    PostCode.SETRANGE(Code,PostCode.Code);
    

    because like Denster says
    are in the "Post Code" table, so every field reference is to the table itself

    Specifically in this situation you are filtering the entire record set PostCode on a field by the value in the current record.
    This is not correct Reinhard. It is difficult because there's something called PostCode (no space, no "") and he is working in the "Post Code" table (spaces and ""). The first one points to the record variable called PostCode, and the second points back at the table he is working in.

    Maybe a more user friendly example would be:
    PostCodeOne.SETRANGE("Search City",TheOtherPostCode."Search City"); 
    PostCodeOne.SETRANGE(Code,TheOtherPostCode.Code);
    
    You use a field value of TheOtherPostCode to set a filter on PostCodeOne.
  • ReinhardReinhard Member Posts: 249
    Denster,
    yes that's true but also not entirely necessary. PostCodeOne could potentially equal TheOtherPostCode and you would still see the "confusing"
    SETRANGE("No.","No.") syntax... so its not that PostCode <> "Post Code" that I think is confusing, but that strangely: "No." is not the same as "No."!

    The first "No." is the name of a field, the second one is a pointer to a specific value.
  • DenSterDenSter Member Posts: 8,307
    @ Reinhard:

    you said
    Reinhard wrote:
    PostCode.SETRANGE("Search City","Search City"); 
    PostCode.SETRANGE(Code,Code);
    
    is a shorter way of doing:
    PostCode.SETRANGE("Search City",PostCode."Search City"); 
    PostCode.SETRANGE(Code,PostCode.Code);
    
    And that is simply not correct.

    The second SETRANGE parameter refers either to Rec, or to the dataitem you are in. It never refers back to PostCode, unless you are in a dataitem that you called PostCode.
  • DenSterDenSter Member Posts: 8,307
    @ Jambo:
    Open NAV, click Help -> C/SIDE reference guide, and search for SETRANGE, that explains it all.
  • ReinhardReinhard Member Posts: 249
    I should have said rec and avoided this but it is good that you kept me from confusing others.
  • DenSterDenSter Member Posts: 8,307
    no problem, it's easy to mix this type of stuff up, I had to look at it more than once to make sure I was looking at it right :wink:
Sign In or Register to comment.