Dynamics 365 Business Central: How to remove or cancel only one filter (Not Reset All filter)

Dynamics 365 Business Central

Hi, Readers.
Recently I saw an interesting question on the Microsoft Dynamics NAV Forum.

Dear All,
Suppose we apply two filters 
rec.setrange(field1,fieldref);//line 1
rec.setrange(field2.fieldref);//line 2
How to disable filter for line 2 by C/AL.

(+) How to disable filters by cal. – Microsoft Dynamics NAV Forum Community Forum

This is actually very simple, but some developers may not have noticed, so in this post, I would like to briefly talk about how to remove or cancel one filter in AL.

PS: The SetRange method provides a quick way to set a simple filter on a field.
Record.SetRange(Any [, Any] [, Any]) Method:

First, you can of course use Record.Reset() Method and then set the filter conditions from the beginning.

Record.Reset() Method: Removes all filters, including any special filters set by MarkedOnly, changes fields select for loading back to all, and changes the current key to the primary key. Also removes any marks on the record and clears any AL variables defined on its table definition.

Take the above problem as an example, three filters are set and only the third one needs to be canceled, but if you use Reset, you need to set the filter1 and filter2 again.

rec.setrange(field1, fieldref);
rec.setrange(field2, fieldref);
rec.setrange(field3, fieldref);
rec.setrange(field1, fieldref);
rec.setrange(field2, fieldref);

So, what is the best way? You just omit the FromValue and ToValue parameters in Setrange, all filters set for that field are removed.

rec.setrange(field1, fieldref);
rec.setrange(field2, fieldref);
rec.setrange(field3, fieldref);

Let’s see more details.
For example:

Test Video:

The conclusion is very simple:
Set filter: Setrange(Field,From,To)
Remove filter: Setrange(Field)

Finally, Microsoft mentioned this way in FieldRef.SetRange([Any] [, Any]) Method, but not in Record.SetRange(Any [, Any] [, Any]) Method.


Hope this will help.

Thanks for reading.