filter using first 5 characters (C/AL Code)

julkifli33julkifli33 Posts: 937Member
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 Posts: 149Member
    VendTable.SETFILTER(VendTable."Search Name",'%1%2',COPYSTR(VendName1,5),'*');
    
    Gerry Kistler
    KCP Consultores
  • julkifli33julkifli33 Posts: 937Member
    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 Posts: 891Member
    VendTable.SETFILTER(VendTable."Search Name",'%1','@'+VendName+'*');
    
    Vijay Gupta
    Changing the code is last step. Try to change processes first...
  • SavatageSavatage Posts: 7,132Member
    COPYSTR(VendName1,5)
    Should be
    COPYSTR(VendName,1,5)
  • gerrykistlergerrykistler Posts: 149Member
    That's what I get for typing the code here rather than in NAV first, then copy and paste.
    Gerry Kistler
    KCP Consultores
  • SavatageSavatage Posts: 7,132Member
    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 Posts: 149Member
    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 Posts: 12,549Member
    and what about
    VendTable.SETFILTER(VendTable."Search Name",'%1*',VendName);
    
    ... ;-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • gerrykistlergerrykistler Posts: 149Member
    Of course, fewer characters so much improved. :D
    Gerry Kistler
    KCP Consultores
  • kinekine Posts: 12,549Member
    No, but correct usage of parameters... ;-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • gerrykistlergerrykistler Posts: 149Member
    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 Posts: 937Member
    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
    Error.jpg
    14K
    Error.jpg 14.4K
  • mohana_cse06mohana_cse06 Posts: 4,848Member
    The error message attached is nothing to do with suggested code..its a setup error..
    activate debugger and check..
  • julkifli33julkifli33 Posts: 937Member
    Hi All,
    it works
    VendTable.SETFILTER(VendTable."Search Name",'%1',VendName + '*');
    

    use this code
    thanks
  • vijay_gvijay_g Posts: 891Member
    Was i wrong..?
    vijay_g wrote:
    VendTable.SETFILTER(VendTable."Search Name",'%1','@'+VendName+'*');
    
    Vijay Gupta
    Changing the code is last step. Try to change processes first...
  • kinekine Posts: 12,549Member
    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.