How to evaluate GETFILTER when using option fields??

Mike_HWGMike_HWG Member Posts: 104
I want to do something like this:
CASE SalesLine.GETFILTER("Document Type") OF
  FORMAT(SalesLine."Document Type"::Order):
    // Do stuff
END;

Let's say I set a filter of Order and run the code. The above doesn't ever evaluate to true because GETFILTER returns 'Order' and the FORMAT returns '1'.

How do I do a correct evaluation? ](*,)
Michael Hollinger
Systems Analyst
NAV 2009 R2 (6.00.34463)

Answers

  • DenSterDenSter Member Posts: 8,307
    Weird how that happens isn't it, but this is one case where you'll have to hard code it.
  • Mike_HWGMike_HWG Member Posts: 104
    I was wondering if it was going to come to that, I just felt wrong for thinking about it :P
    Michael Hollinger
    Systems Analyst
    NAV 2009 R2 (6.00.34463)
  • DenSterDenSter Member Posts: 8,307
    Yeah it does feel all wrong, and it is, but when you don't have a choice....
  • David_SingletonDavid_Singleton Member Posts: 5,479
    Mike_HWG wrote:
    I want to do something like this:
    CASE SalesLine.GETFILTER("Document Type") OF
      FORMAT(SalesLine."Document Type"::Order):
        // Do stuff
    END;
    

    Let's say I set a filter of Order and run the code. The above doesn't ever evaluate to true because GETFILTER returns 'Order' and the FORMAT returns '1'.

    How do I do a correct evaluation? ](*,)

    try this
    salesheader.reset;
    salesheader.copyfilters(rec);
    for i := 0 to 5 do begin
      salesheader2."Document Type" := i;
      if salesheader.getfilter("document type") = strsubstno('%1',salesheader2."Document Type") then
        CASE salesheader2."Document Type" OF
          salesheader."Document Type"::quo :
            message('You filtered on quote');
          salesheader."Document Type"::order :
            message('You filtered on Order');
          salesheader."Document Type"::invoice :
            message('You filtered on invoice');
          salesheader."Document Type"::cre :
            message('You filtered on Credit Memo');
        END;
    end;
    
    David Singleton
  • kinekine Member Posts: 12,562
    Or of couse, you can use the third parameter of the FORMATfunction to return string representation instead number... ;-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • David_SingletonDavid_Singleton Member Posts: 5,479
    kine wrote:
    Or of couse, you can use the third parameter of the FORMATfunction to return string representation instead number... ;-)

    Even better. :thumbsup:
    David Singleton
  • DenSterDenSter Member Posts: 8,307
    kine wrote:
    Or of couse, you can use the third parameter of the FORMATfunction to return string representation instead number... ;-)
    Can you share how exactly? I tried a bunch of them, for instance:
    MESSAGE(FORMAT(SalesHeader."Document Type"::order,0,'<Text,0>'));
    MESSAGE(FORMAT(SalesHeader."Document Type"::order,0,'<Text,1>'));
    MESSAGE(FORMAT(SalesHeader."Document Type"::order,0,0));
    MESSAGE(FORMAT(SalesHeader."Document Type"::order,0,1));
    MESSAGE(FORMAT(SalesHeader."Document Type"::order,0,'<Standard Format,1>'));
    
    They all show the number:
    Microsoft Dynamics NAV Classic
    1
    OK

    None of them show the text representation of the option field. By this time I would have lost patience and just hardcoded the value :)
  • kinekine Member Posts: 12,562
    Ah, I understand, the problem is, that you are formating the constant, whcih is INTEGER in result, thus still getting the number. If you use variable of type option and you will set the value into it and format it, you will get the option text.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • David_SingletonDavid_Singleton Member Posts: 5,479
    DenSter wrote:

    None of them show the text representation of the option field. By this time I would have lost patience and just hardcoded the value :)

    Mine works :mrgreen:
    David Singleton
  • MBergerMBerger Member Posts: 413
    Mine works :mrgreen:
    It does, but it assumes you only filter on one document type, and in that case you can just use the first document type you find.
    salesheader.reset;
    salesheader.copyfilters(rec);
    if salesheader.findset then
      if salesheader.getfilter("document type") <> '' then
        message('You filtered on document type %1',format(salesheader."document type")) ;
    

    Note to self.. Pressing F11 in a browser doesn't compile ;)
  • David_SingletonDavid_Singleton Member Posts: 5,479
    MBerger wrote:
    Mine works :mrgreen:
    It does, but it assumes you only filter on one document type, and in that case you can just use the first document type you find.

    But that is an entirely different issue CASE can't ever work like a filter. No matter what you would need to break up the comma string. I think you are confusing two different problems into one. I have shown you how to solve the question you asked, now you need to address the next one. I can see plenty of ways to solve it. For a small number of options like yours I would create an Array or options, for a larger selection I would use a temporary table.
    David Singleton
  • David_SingletonDavid_Singleton Member Posts: 5,479
    As a hint you are going to need something like:
    salesheader.reset;
    salesheader.copyfilters(rec);
    for i := 0 to 5 do begin
      salesheader2."Document Type" := i;
      OptionFound[i] := STRPOS(strsubstno('%1',salesheader2."Document Type"),salesheader.getfilter("document type")) <> 0 
     ....
    
    David Singleton
  • Mike_HWGMike_HWG Member Posts: 104
    David, I think there's a point where the complexity is going to outweigh hard-coding... at least at this point with the tools we have. Thinking about switches like <>, .., &, *, and so on, you will eventually get yourself into building a parser. I guess you could create a really good set of functions built for this and add it to your 'tool bag' codeunit... let me know when I can download it :lol::wink:
    kine wrote:
    Ah, I understand, the problem is, that you are formatting the constant, which is INTEGER in result, thus still getting the number. If you use variable of type option and you will set the value into it and format it, you will get the option text.

    Umm... I don't think I'm getting it, because this gives me '1' as well:
    MyOption := Salesheader."Document Type"::Order;
    MESSAGE(FORMAT(myoption));
    MESSAGE('%1',MyOption);
    
    Michael Hollinger
    Systems Analyst
    NAV 2009 R2 (6.00.34463)
  • David_SingletonDavid_Singleton Member Posts: 5,479
    Mike_HWG wrote:
    David, I think there's a point where the complexity is going to outweigh hard-coding...

    I see now that the post I replied to before was not yours. I think Mberger has just taken the whole thread down a completely different direction and added confusion.

    To MBerger, a CASE command can NOT handle multiple valid options, it gets to the first one then exits.

    Mike if all you want is what you asked in your original post then my code does exactly what you want.
    David Singleton
Sign In or Register to comment.