filter using first 5 characters (C/AL Code)

julkifli33julkifli33 Member Posts: 1,073
hi all
usually when we want to filter using '*'
but how to do that in CAL code?
let say we have vendor name : microsoftdynamics
but i just have parameter micro
how to filter using first 5 characters?
thanks
VendTable.SETFILTER(VendTable."Search Name",'%1%2',VendName,'*');
-->failed

Answers

  • gerrykistlergerrykistler Member Posts: 149
    VendTable.SETFILTER(VendTable."Search Name",'%1%2',COPYSTR(VendName1,5),'*');
    
    Gerry Kistler
    KCP Consultores
  • julkifli33julkifli33 Member Posts: 1,073
    VendTable.SETFILTER(VendTable."Search Name",'%1%2',COPYSTR(VendName1,5),'*');
    

    failed also

    my complete code
              VendName := COPYSTR(FORMAT(COPYSTR(vString,1,PosCode-1)),1,5);
              VendTable.RESET;
              VendTable.SETFILTER(VendTable."Search Name",'%1%2',VendName,'*');
              IF VendTable.FIND('-') THEN
    
  • vijay_gvijay_g Member Posts: 884
    VendTable.SETFILTER(VendTable."Search Name",'%1','@'+VendName+'*');
    
  • SavatageSavatage Member Posts: 7,142
    COPYSTR(VendName1,5)
    Should be
    COPYSTR(VendName,1,5)
  • gerrykistlergerrykistler Member Posts: 149
    That's what I get for typing the code here rather than in NAV first, then copy and paste.
    Gerry Kistler
    KCP Consultores
  • SavatageSavatage Member Posts: 7,142
    How you you want to specify the first 5 digits?

    Do you want a TEXTBOX where you enter 5 characters & then it filters?
    Do you want to click on a name and it takes "that" name - get's the first 5 characters and then filters for others with the same starting 5?

    Not sure what your final goal is? :-k
  • gerrykistlergerrykistler Member Posts: 149
    I see now it is the %1%2 which is the problem. Try this:
          VendTable.SETFILTER(VendTable."Search Name",'%1',VendName + '*');
    
    Gerry Kistler
    KCP Consultores
  • kinekine Member Posts: 12,562
    and what about
    VendTable.SETFILTER(VendTable."Search Name",'%1*',VendName);
    
    ... ;-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • gerrykistlergerrykistler Member Posts: 149
    Of course, fewer characters so much improved. :D
    Gerry Kistler
    KCP Consultores
  • kinekine Member Posts: 12,562
    No, but correct usage of parameters... ;-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • gerrykistlergerrykistler Member Posts: 149
    Yes, 6 fewer keystrokes. Both are correct and work but your's is preferred for less code space.

    Note for the OP (julkifli33) - next time give your code sample along with the question, as your question was somewhat misleading as to the issue. Also any error message you received such as "No vendor found in the filter '%1%2'". You will get your answers sooner.
    Gerry Kistler
    KCP Consultores
  • julkifli33julkifli33 Member Posts: 1,073
    if i use it's ok, but it should have the exact name
    VendTable.SETRANGE(VendTable."Search Name",VendName);
    

    because i want to import for 3rd party
    even the same vendor, but i think its better if i search from the first 5 characters
    because this 3rd party dont have vendor id

    i use this code
    VendTable.SETFILTER(VendTable."Search Name",'%1',VendName + '*');
    
    or
    VendTable.SETFILTER(VendTable."Search Name",'%1*',VendName);
    

    the error message as i attach
  • mohana_cse06mohana_cse06 Member Posts: 5,503
    The error message attached is nothing to do with suggested code..its a setup error..
    activate debugger and check..
  • julkifli33julkifli33 Member Posts: 1,073
    Hi All,
    it works
    VendTable.SETFILTER(VendTable."Search Name",'%1',VendName + '*');
    

    use this code
    thanks
  • vijay_gvijay_g Member Posts: 884
    Was i wrong..?
    vijay_g wrote:
    VendTable.SETFILTER(VendTable."Search Name",'%1','@'+VendName+'*');
    
  • kinekine Member Posts: 12,562
    Offtopic:
    VendTable.SETFILTER(VendTable."Search Name",'@%1*',VendName);

    Guys, what's wrong on using the SETFILTER correctly? Filter in second parameter, values, in rest... do not forget that 3rd, 4th... parameters are strongly typed based on the field type. Second parameter is there to create filter with placeholders for values. I know, that there are sometime problems with the placeholders not replaced with the values, but it is just exceptional behavior and could be solved in exceptional way.

    Sorry for this offtopic post, but I can't resist...

    (same for MESSAGE(FORMAT(something)) instead MESSAGE('%1',something))
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
Sign In or Register to comment.