Dynamically SETFILTER

Marcel_BMarcel_B Member Posts: 3
Hello everyone,

One of the departments in our company has requested a change that makes me need to be able to dynamically create a text variable that I can use as a filter, like this:
ItemGRec.SETFILTER("Variant Filter",MyDynamicFilterGTxt);

MyDynamicFilterGTxt needs to contain both the empty Variant Code, as well as a varrying number of filled Varinat Codes, which is why I was trying to build something along the lines of this:
ItemGRec.SETFILTER("Variant Filter",'%1|%2|%3','',VariantCode1,VariantCode2);

But since the filter will have to be set in various places all over the codeunit, I wanted to put the '%1|%2|%3','',VariantCode1,VariantCode2 part in a text variable.
So I have been trying to accomplish that like this:
FirstPartGTxt := '''%1|%2|%3''';
SecondPartGTxt := ''',Variant1,Variant2';
MyDynamicFilterGTxt := FirstPartGTxt + ',' + SecondPartGTxt;
ItemGRec.SETFILTER("Variant Filter",MyDynamicFilterGTxt);

But then it tells me: Filter ''%1|%2|%3','',Variant1,Variant2' is invalid.

I am working on a 3.70B version of Navision. Can anyone tell me if what I am trying to do is possible at all, and if so, what I am doing wrong, and how it should work?

Thanx a lot for your time and effort! :D
Marcel

Comments

  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    Marcel_B wrote:
    FirstPartGTxt := '''%1|%2|%3''';
    SecondPartGTxt := ''',Variant1,Variant2';
    MyDynamicFilterGTxt := FirstPartGTxt + ',' + SecondPartGTxt;
    ItemGRec.SETFILTER("Variant Filter",MyDynamicFilterGTxt);
    No, you can't use it in a way like this. That would mean your place holders are part of the filter expression itself. Try to build your filter like this
    MyDynamicFilterGTxt := MyDynamicFilterGTxt + '|' + NextPart;
    
    or try to use STRSUBSTNO.
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • Marcel_BMarcel_B Member Posts: 3
    Dumb of me to forget about STRSUBSTNO. :oops:

    Now it seems to work when I use this:
    MyDynamicFilterGTxt := 'Variant1|Variant2';
    ItemGRec.SETFILTER("Variant Filter",STRSUBSTNO('%1|%2','''''',MyDynamicFilterGTxt));

    \:D/
  • jversusjjversusj Member Posts: 489
    Marcel_B wrote:
    Dumb of me to forget about STRSUBSTNO. :oops:

    Now it seems to work when I use this:
    MyDynamicFilterGTxt := 'Variant1|Variant2';
    ItemGRec.SETFILTER("Variant Filter",STRSUBSTNO('%1|%2','''''',MyDynamicFilterGTxt));

    \:D/

    thanks, this helped me tremendously today, but i went even further with a need to dynamically set the %1|%2|...|%X portion of the filter, and ended up using
    table.SETFILTER(Field,STRSUBSTNO(FilterString,FilterStringValues))

    I am not too concerned about string overflows at this time, which I saw others warning about in threads about dynamically building a filter. Thanks for tip!
    kind of fell into this...
Sign In or Register to comment.