0

I am trying to execute below query and getting error which says Unexpected token IN Can someone please help to understand what is wrong here.

String str = 'SELECT Name FROM Account';
String str1='WHERE';
String str3='Name IN (\'test\',\'test9\')';
list<Account> accclist=Database.query(str+str1+str3);
system.debug(accclist);

However when I run this below one it runs perfectly fine.

list<Account> accclist=Database.query('SELECT Name FROM Account WHERE Name IN (\'test\',\'test9\')');
1
  • 3
    You do not need to use a dynamic soql query here, it's only making things more complicated for you. The general advice for everyone is to use static queries until you know that you can't use them.
    – Derek F
    Mar 27 at 12:41

1 Answer 1

1

There are multiple issues in your code.

1.Add spaces before WHERE & Name clauses.

2.Since you are using single quotes in condition , you will need to escape it as below :

str3='Name IN (\'test\',\'test9\')';

More info - https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_quotedstringescapes.htm

3.Also , Database.query returns List of sObjects which needs type casting.

Final code will look as below:

String str = 'SELECT Name FROM Account'; 
String str1=' WHERE'; 
String str3=' Name IN (\'test\',\'test9\')'; 
List<Account> accclist=(List<Account>)Database.query(str+str1+str3); 
system.debug(accclist);

Please refer the guide for more info.

5
  • the values under IN will be received dynamically(i.e. test and test9)..and I need to store then in a string and pass it on to the final string which is string3...I tried this option but getting error. Please help to get this resolved as this is required urgently: String str = 'SELECT Name FROM Account'; String str1=' WHERE'; String strnew='test'; String new1='test9'; String str3=' Name IN ('\''+strnew+'\''+','+'\''+new1+'\')'; List<Account> accclist=(List<Account>)Database.query(str+str1+str3); system.debug(accclist); Mar 27 at 15:29
  • You can use below code, here we are using list with variable binding in dynamic SOQL : String str = 'SELECT Name FROM Account'; String str1=' WHERE'; List<String> names = new List<String>{'test','test9'}; for(String str: names) { str = '\''+str+'\''; } String str3=' Name IN :names'; List<Account> accclist=(List<Account>)Database.query(str+str1+str3); system.debug(accclist);
    – Rohit
    Mar 27 at 15:40
  • I remodified the code like below and it worked..thanks for the help so far: String str = 'SELECT Name FROM Account'; String str1=' WHERE'; String strnew='test'; String new1='test9'; String str3=' Name IN'+ '('+'\''+strnew+'\''+','+'\''+new1+'\')'; List<Account> accclist=(List<Account>)Database.query(str+str1+str3); system.debug(accclist); Mar 27 at 15:40
  • ok . I had also added it in my previous comment. But as @DerekF pointed out, we do not need dynamic SOQL in this scenario.
    – Rohit
    Mar 27 at 15:41
  • 1
    Understood..Thanks @Rohit for your help and advice in this regard. Mar 27 at 15:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .