C/AL Code Filter Error w/ Equal Sign and Bracketing

headley27headley27 Member Posts: 188
I need to filter a list of items using C/AL Code.

I have a problem where the code errors when an item containing an equal sign is filtered. The same error occurs with bracketing.

For example part number ABC123(v2) would fail.
This occurs when manually applying a filter as well.

I have found that surrounding the string with quotes 'ABC123(v2)' fixes the issue when applying the filter manually.

How do I pass quotes into my variable?

Rec = Current Record: Item Table
SKU = Record Variable (Stockkeeping Unit Table)

Code

FilterNo := Rec."No.";
SKU.SETFILTER("Item No.", FilterNo);

End Code

I need my FilterNo variable to work like this:
SKU.SETFILTER("Item No.", 'ABC123(v2)');
Not
SKU.SETFILTER("Item No.", ABC123(v2));

Can someone please guide me in the right direction?

Thank you,
headley27

Comments

  • lakshmivallurulakshmivalluru Member Posts: 168
    item.setfilter("No.",'''TEST(ITEM)''');

    All ''' are three single quotes.

    But how is this going to work if you have such special charecters in item no. ?? It will fail everywhere the item is used.
    LR
  • kinekine Member Posts: 12,562
    The source of your problem is mis-using SETFILTER. YOu need to know that there is BIG difference between
      SETFILTER(Field,'something');
    

    and
      SETFILTER(Field,'%1','something');
    

    The first one apply 'something' as filter (plain filter), second one apply string 'something' like value you want to filter... Please, use first option just only if you have some filter saved in variable and you want to apply this filter. In other cases use the second option or use
      SETRANGE(Field,Value);
    

    if you are filtering for one value...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • headley27headley27 Member Posts: 188
    Thank you kine.

    That's exactly what I was looking for.

    In the meantime, I had written some code to escape single quotes that existed in my variable. I then surrounded the variable with single quotes and passed it into the SetFilter function.

    This worked for me using 7 lines of code but your solution gives me the same results using 1.
    SKU.SETFILTER("Item No.",'%1',FilterNo);

    Beautiful.

    Thanks again,
    headley27
Sign In or Register to comment.