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
Linda 98Linda 98 

Picklist in Soql in batch apex

Hi

Some where i am going wrong and i keep on getting tis error.
I am writing a batch apex and i just want to query opportunities whose stage is not WON.

This is what i had written and it gives me error saying 'execpted semi colon found something or other'

Here is my part of code.Please correct me .I used !=.
I tried using IN,Includes,Exculdes.
But no Luck.
global Database.QueryLocator start(Database.Batchablecontext BC){
    query='select id from Opportunity where stage != 'WON' ';
    return Database.getQueryLocator(query);

Thank you!
 
David ZhuDavid Zhu
query='select id from Opportunity where stagename != \'WON\' ';
KaranrajKaranraj
If you are using single quotes ' in query string then you need to use the \ and then your singlequotes '
Try the below code 
global Database.QueryLocator start(Database.Batchablecontext BC){
    query='select id from Opportunity where stage != \'WON\' ';
    return Database.getQueryLocator(query);

 
JWykelJWykel
See both David Zhu and Karanraj's answers and combine them.
The field is 'StageName'
If you're using dynamic SOQL, you must escape the single quotes:
query = 'SELECT Id FROM Oppertunity WHERE StageName != \'WON\'';