Checking/Comparing for empty string filter

xHorizonxHorizon Member Posts: 4
Hi there,

today I stumbled upon something I thought would be a simple problem but I couldn't figure out why it did not work.
So I wrote some code which checks if specific filters on a Reservtion Entry record are set (those filters are set in the base app)
And one of those filters is a filter set on the field "Source Batch Name" of the Reservation Entry, it is set to an empty string.
So the Source Batch Name filter is actually build like that
ReservationEntry.SetFilter("Source Batch Name", '');
In my event subscriber I then check if the Reservation Entry's filter on various fields are of specific values, however my condition on the "Source Batch Name" to check if the filter indeed is set to an empty string is always false.
The code where I check for the empty string looks something like this:
SourceBatchNameFilter := ReservationEntry.GetFilter("Source Batch Name");
if SourceBatchNameFilter = '''' then
  //DoSomething...
To my surprising this expression is always false even though the debugger is clearly showing me that the value of "SourceBatchName" = '''', I also tried to copy the value directly form the source of the variable with "copy value" and changed the condition to check for '' instead of '''' but to no avail.
I also compared the two texts via text comparision and there they are equal, so is this because of how strings are escaped in AL?
I also tried to negate the expression, still the expression always results in false like that:
if not (SourceBatchNameFilter <> '''') then
  //DoSomething...
So how would I be able to check if an explicit empty filter string was set on a field when you only have the option to get the value out of the filter by calling .GetFilter?

Best Answer

  • vaprogvaprog Member Posts: 1,113
    Answer ✓
    So, you set the filter to be empty and then expect the filter to distinguish an empty string from any other value. Isn't that unreasonable?

    If you want to filter for a distinct value, you should always use SETRANGE. SETRANGE automatically quotes the value for you (or does behind the scenes what proper quoting would do).

    If you can't, or don't want to use SETRANGE, you should use replacement placeholders. In your case this would be
    SETFILTER("Source Batch Name",'%1','')
    
    . This is, however, tricky, as certain filter meta characters change the way values are folded into the filter string. In some cases, proper quoting applies, in other cases the same thing happens as if you used STRSUBST to create the filter string parameter, i.e.
    SETFILTER(Field,STRSUBST('%1*','ABC'))
    
    and in even other cases what looks like a placeholder is treated as literal value.

    So, in essence, GETFILTER returns '' because you SETFILTER ''. If you want to GETFILTER '''''''' (i.e. the filter expression
    ''
    
    ), you need to set the filter accordingly.

    Check out the documentation on setting filter strings (ignore the section on the quick filter, unless you want to analyze how the system mangles your input by looking at the Filters section on the "About this page" page; take into account that you need to double all single quotes that are part of the filter string and surround the whole string with single quotes, in order to use it as a literal string in C/AL Code) and my analysis of how SETFILTER's parameters are treated.

Answers

  • KTA8KTA8 Member Posts: 385
    make the if with simple '' instead
  • vaprogvaprog Member Posts: 1,113
    Answer ✓
    So, you set the filter to be empty and then expect the filter to distinguish an empty string from any other value. Isn't that unreasonable?

    If you want to filter for a distinct value, you should always use SETRANGE. SETRANGE automatically quotes the value for you (or does behind the scenes what proper quoting would do).

    If you can't, or don't want to use SETRANGE, you should use replacement placeholders. In your case this would be
    SETFILTER("Source Batch Name",'%1','')
    
    . This is, however, tricky, as certain filter meta characters change the way values are folded into the filter string. In some cases, proper quoting applies, in other cases the same thing happens as if you used STRSUBST to create the filter string parameter, i.e.
    SETFILTER(Field,STRSUBST('%1*','ABC'))
    
    and in even other cases what looks like a placeholder is treated as literal value.

    So, in essence, GETFILTER returns '' because you SETFILTER ''. If you want to GETFILTER '''''''' (i.e. the filter expression
    ''
    
    ), you need to set the filter accordingly.

    Check out the documentation on setting filter strings (ignore the section on the quick filter, unless you want to analyze how the system mangles your input by looking at the Filters section on the "About this page" page; take into account that you need to double all single quotes that are part of the filter string and surround the whole string with single quotes, in order to use it as a literal string in C/AL Code) and my analysis of how SETFILTER's parameters are treated.
  • xHorizonxHorizon Member Posts: 4
    vaprog wrote: »
    So, you set the filter to be empty and then expect the filter to distinguish an empty string from any other value. Isn't that unreasonable?
    Yes it would be, but its not me that is setting the filter as an empty string, but is done in the base app as described by me. I just wanted to show the example of how it is set.
    I justed wanted to check for certain filters that are set on the reservation record, to then hook in with my event subscriber to trigger some code when certain filters are set.

    So the literal string I compared the result of the GetFilter expression to wasn't escaped properly by me as you mentioned hence the to strings didnt match, thanks for the detailed overview of how the filters are applied by the SETRANGE/SETFILTER methods.
Sign In or Register to comment.