SETRANGE vs SETFILTER

What is the difference between the SETRANGE and SETFILTER functions?

Under what circumstances should I be using each? If I look at the C/SIDE help text, the descriptions are virtually identical and there is no real indication as to their intended application.

Hi Garry, CommentsLine.SETFILTER (“Table Name”, ‘Customer’); SalesInvLine.SETFILTER (“Document No.”, ‘<>%1’, ‘’); //Not Blank SalesInvLine.SETFILTER (Type, ‘<>%1’, SalesInvLine.Type::""); //The way to cope with blank types. SalesInvLine.SETFILTER (Type, ‘%1|%2’, SalesInvLine.Type::Item, SalesInvLine.Type::“Account (G/L)”); //’|’ for an OR expression, ‘&’ for an AND expression. StockItem.SETFILTER (“No.”, ‘%1…%2’, ‘AAA’, ‘ZZZ’); N.B., To reset a previous filter: Either: CommentsLine.SETFILTER(“No.”,’’); Or: CommentsLine.SETRANGE(“No.”); N.B., To set a filter for to find blank records: CommentsLine.SETFILTER(“No.”,’%1’,’’); N.B., To find a record, where only part of the record is known: CustomerRec.SETFILTER(Name,’%1’,“Inv. Name”+’’); N.B., To find part of a record, case insensitive: SETFILTER(Surname,’%1’, '@’+SurnameSearch+’*’); SETFILTER(Name,’%1’, ‘@temp’); N.B., To find a record which has unusual characters in, i.e., an ‘&’: Transactions.SETFILTER (“Description”, ‘%1’, ‘David&Goliath’); In other words Garry, the World’s your oyster, use whichever one you feel happith with, just as long as you know the differences between each one. cheers David

quote:

What is the difference between the SETRANGE and SETFILTER functions? Under what cirumstances should I be using each? If I look at the C/SIDE help text, the descriptions are virtually identical and there is no real indication as to their intended application.
Originally posted by SkippyKGS - 2005 Jan 24 : 14:57:45

SETRANGE only accepts values - a single value or a range of values(minimum, maximum) of the same type of the field on which the filter will be applied; SETFILTER accepts a string containing a filter expression Anna

Cheers for those quick replies. Its just one of those things where I was using SETFILTER for everything and didn’t really know why I wasn’t using SETRANGE. No harm done then. Thanks again.

quote:

SETRANGE only accepts values - a single value or a range of values(minimum, maximum) of the same type of the field on which the filter will be applied;
Originally posted by Anna Perotti - 2005 Jan 24 : 15:06:16

… or NO value at all (to ‘reset’ the range).

I think that the difference is in performance, because SETFILTER must parse the filter you type in the second argument, SETRANGE must not. So in my code I never use SETFILTER if I can use SETRANGE. Example: I write SETRANGE(“Posting Date”,FromDate); and not SETFILTER(“Posting Date”,’%1’,FromDate); I think that the second one is slower, but I’m not sure of this. Bye Marco

SETRANGE is a convenience for the programmer. It maps the parameters internally to the correct filter expression and calls a filter function - the same one that SETFILTER uses. So SETRANGE is just a wrapper of SETFILTER that involves an additional c++ function call (not measurable). No performance or funtional difference to be concerned about - its just a simplification. If you can express your filter with SETRANGE do that, otherwise use SETFILTER.

There have been a few discussions around this topic (and even a poll!): http://www.mbsonline.org/forum/topic.asp?TOPIC_ID=6307&SearchTerms=setrange,setfilter Quoting David Singleton in that same topic, here’s exactly what Robert just confirmed above:

quote:

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.
Originally posted by David Singleton - 2003 Apr 06 : 05:49:39

An interesting topic in the ‘ChangesInNAVW14.0.doc’ on the v4.0 release cd. Topic A275) Usage of SETFILTER in CRM area “When setting a filter in C/AL, you should always consider using SETRANGE before using SETFILTER. Or, in other words, don’t use a filter if it’s actually a from-to range restriction the program should work on. The reason for this is that the SETRANGE command doesn’t interpret the FromValue and the ToValue. The SETFILTER does interpret the values in the filter string. This means that if, for example, the FromValue is “AT&T”, the “&” will be interpreted as AND in the SETFILTER command, but it would not, if the SETRANGE command was used. Another reason is to make the code more consistent and thereby easier to read.”

This is half right in that it points to a subtle difference with wildcards. If you say, as with that example: SETRANGE(‘Code’, ‘AT&T’); or SETFILTER(‘Code’, ‘%1’, ‘AT&T’); You obtain the same result. Both will match AT&T exactly. The ‘&’ is not regarded as an AND operator when used in the value, only in the expression, so: SETFILTER(‘Code’, ‘%1&%2’, ‘A’, ‘T’); will of course try to match both A AND T, which will not match a record. However, the difference is with wildcards. SETRANGE does not respect wildcards in the value and SETFILTER does, so SETRANGE(‘Code’, ‘AT’); will try to match 'AT’ exactly and would not find, for example, ‘AAT’. But: SETFILTER(‘Code’, ‘%1’, ‘A*T’); will use the wildcard and match AAT.

These are the exact kinds of intricacies and qualifications I was after. Thanks a lot and keep it up guys!

Robert, Thank you for the detailed clarification.

Why does setfilter not work with date fields? Ex. SalesLine.Setfilter(“Shipment Date”<=“Shipment Date”);

quote:

Why does setfilter not work with date fields? Ex. SalesLine.Setfilter(“Shipment Date”<=“Shipment Date”);
Originally posted by mblauer - 2005 Aug 30 : 21:02:21

Dates works just fine like this: SalesLine.SETFILTER(“Shipment Date”, ‘…%1’, MyDateVar); //Lars

hmm… I’m sure we already had this discussion, but… Basically SETFILTER is the Navision function that internally … sets filters [Duh!] where as SETRANGE is a function that does multiple things, and is bassically a parser to the Navison setfilter function. With Set filter you can do anything you want, but you have to set the paramaters carefully. SETRANGE just allows you to do short cuts. In the case of SalesLine.Setfilter(“Shipment Date”<=“Shipment Date”); I am very surprised that you can even compile this , since it is really a complete load of nonsense.

Thanks for this thread!:slight_smile:

Now I know the difference between the two!hehe