+ Start a Discussion
mxalix258mxalix258 

Help with SOQL statement

Could anyone help with this SOQL statement? It seems basic, but I can't get it right for some reason.

 

    String soql = 'select id, name from account';
    if(searchString != '' && searchString != null)
      soql = soql +  ' where name LIKE \'%' + searchString +'%\'';
    soql = soql + ' limit 25';

 all I'm trying to do is an an additional condition on the statement to say something like Industry = 'Education'

 

Any help would be appreciated!

Best Answer chosen by Admin (Salesforce Developers) 
m.elmoussaouim.elmoussaoui

 

    String soql = 'select id, name from account';
    if(searchString != '' && searchString != null)
      soql = soql +  ' where Industry = \'Education\' AND name LIKE \'%' + searchString +'%\'';
    soql = soql + ' limit 25';

now it should work.

All Answers

m.elmoussaouim.elmoussaoui
what's the exact error?
mxalix258mxalix258

When I do something like shown below, it gives me an error like "Was expecting Semi-colon, received "Education" or something like that...and no matter how I configure it I can't get it to work.

 

    String soql = 'select id, name from account';
    if(searchString != '' && searchString != null)
      soql = soql +  ' where Industry = 'Education' AND name LIKE \'%' + searchString +'%\'';
    soql = soql + ' limit 25';
m.elmoussaouim.elmoussaoui

 

    String soql = 'select id, name from account';
    if(searchString != '' && searchString != null)
      soql = soql +  ' where Industry = \'Education\' AND name LIKE \'%' + searchString +'%\'';
    soql = soql + ' limit 25';

now it should work.

This was selected as the best answer
mxalix258mxalix258
Thank you!