function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Stephen E 4Stephen E 4 

What is wrong with this SOQL Query?

Hello!

I am trying to write some SOQL so that I can perform some bulk operations on opportunties. However, whenever I run this SOQL I get an error: "unexpected token: AND"

Here is the SOQL:
 
String query2 = 'SELECT Id FROM Opportunity WHERE NOT StageName LIKE \'%'+'closed'+'%\' AND division__c = \'IT Solutions\'';

Here is the System.debug output:
 
SELECT Id FROM Opportunity WHERE NOT StageName LIKE '%closed%' AND division__c = 'IT Solutions'

I am just unsure what is wrong with this SOQL, seems I am escpaing the single quotes correctly... 

Thank you!
Best Answer chosen by Stephen E 4
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Stephen ,

Can you please try below code .
String query2 = 'SELECT Id FROM Opportunity WHERE division__c = \'IT Solutions\'' AND (NOT StageName LIKE \'%'+'closed'+'%\' );
Please Mark it as BEST ANSWER if it helps you .


 

All Answers

Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Stephen ,

Can you please try below code .
String query2 = 'SELECT Id FROM Opportunity WHERE division__c = \'IT Solutions\'' AND (NOT StageName LIKE \'%'+'closed'+'%\' );
Please Mark it as BEST ANSWER if it helps you .


 
This was selected as the best answer
Charan Thumma 15Charan Thumma 15
HI Stephan,

Try this one 

SELECT Id FROM Opportunity WHERE division__c = \'IT Solutions\' AND (NOT StageName like \'closed\' )
Stephen E 4Stephen E 4
Thank you both! you both were correct, I was forgetting the parentheses.