Dynamics User Group - Archived Forums

The forums in this section of DUG are no longer accepting new post, but you can still get lots of value from the old posts here.
Please visit the active forums to comment/post new questions (choose which product you are interested in):

Setting filters :)

Which code do you prefer?

Integer and Field is of Integer type
  • Of course the second line of code is better!
  • Either could be better depending on context. I also expect a better code than one where a variable is a key word, so I dont vote on those two.

  • This poll is more about which code do you use in your life Smile
  • It depends of the situation.
    Sometimes you cannot use SETRANGE and have to do it with SETFILTER.
    But i prefer SETRANGE.
  • I don't like SETRANGE in this situation just for one reason - filter on this Field looks like this:

    But with SETFILTER it looks normal and is easier to understand for developers and users. I had some trouble with it before so that's why I made poll Smile That's what I think.
  • In this case i woud prefer the second code because your going on an integer :D IF it was a key-word (or something similar) i would use the first one.
    Usually i prefer SETFILTER but not on Numbers!
  • Two Different Uses

    If the field is part of the Key set by Record.RESET or Record.SETCURRENTKEY use SETRANGE else use SETFILTER

    If the field is in the key use

    If the field is not in the key use

    If the field is in the Key but you wish to select a range and exclude the current record by filter use both take a Document Line
    Record.SETRANGE("Document Type","Document Type");
    Record.SETRANGE("Document No.","Document No.");
    Record.SETFILTER("Line No.",'<>%1',"Line No.");

    The above code excludes the saved current record from the filtered set which you canot do with SETRANGE.
    You would maybe use this to get a total Amount if the Amount in rec is different to xrec


    // Exit with the new Total Amount
    EXIT(Record.Amount + Rec.Amount);

  • It doesn't matter if it's part of the key or not for using SETRANGE or SETFILTER....
    The most easy way is:
    If the filter is just a value (example all records where code = 'blue') then use setrange.
    If the filter is a simple interval (example blue..red ), then you can also use setrange.
    If the filter is a negation or a more complex filter (AND,OR...) use SETFILTER;

  • I prefer to use SETRANGE whenever possible. I think in a training i heared that SETRANGE would be faster than SETFILTER.
    But - speed is often not the only citeria for coding it this way. If i'm ill or absent a colleague of me has to maintain my code. So it's necessary to write code so anybody else can understand asap what i wanted to do. Hence i like to create s unit in a way it is readable even month or years later... Wink
  • Internally Navision does not have a "SETRANGE" function, in fact at compile time the SETRANGE function is compiled as a SETFILTER command. So in reality it makes no difference.

    My personal preference is that SETRANGE is easier to read, so I use it where ever possible.
  • Suppose you have a form with a filter to select a country code. Let's call the variable FormCountryFilter.

    When you type for example:

    Then the filter will be reset if the user leaves the FormCountryFilter blank. So, here you will get all customers.

    When you type:

    Then the filter will be set to equal to '' if the user leaves the FormCountryFilter blank. So, here you will get all Customer records with a blank Country field.

    Conclusion: There can be a significant difference in the usage of SETRANGE and SETFILTER
  • But if you type:
    Then it will be the same as SETRANGE :) This also will work same way:

  • the second one.
    also in my opinion it takes more time to read a setfilter sentence than a setrange sentence... but this is just another opinion.

    i choose setrange.
  • both lines makes same effect
    the second one is shortest that all!!
  • quote:
    Originally posted by sebastien.bas-guasch

    both lines makes same effect
    the second one is shortest that all!!

    Nice lets see your SETRANGE for Countries AT & BE & CZ & DE

    Range of Records
    SETRANGE("Country Code",AT,DE); (AT..DE)

    Filtered Selection of Records
    SETFILTER("Country Code",'%1','AT|BE|CZ|DE');