0

I wrote below query for single status check, it is working but not for multiple status checks, it is throwing errors.

String draft = 'Draft';
String cancel = 'Cancelled';
String cmpnQuery = 'SELECT ID,Name,FROM Campaign where status!=:draft'; //working without issues

 String campQuery = 'SELECT ID,Name FROM Campaign where status NOT IN (draft,cancel)'; // not working

for(Campaign cmpn:Database.Query(cmpnQuery )){
//TO- DO
}
2
  • What error(s) are you getting? Please edit your question to include them. It could be that you simply need to add some (escaped) quotes around your target status values in the second query string.
    – Derek F
    Sep 20, 2019 at 16:15
  • draft,cancel are string values so use a list and bind that here in the same way you did in last query Set<String> discardStatuses = new Set<String>{'draft', 'cancel'} and use it like 'SELECT ID,Name FROM Campaign where status NOT IN :discardStatuses Sep 24, 2019 at 19:46

3 Answers 3

4

You should make a Set<String> and add the status to it. Like this

Set<String> stringSet = new Set<String>{'draft','cancel'};

Then you can update the query to:

String campQuery = 'SELECT ID,Name FROM Campaign where status NOT IN: stringSet';
3

Use Quoted String Escape Sequences character

Database.query('SELECT ID,Name FROM Campaign where status NOT IN (\'draft\',\'cancel\')')
0

Sarvesh and Zach are good anwsers, you can also achieve the same result with the next query.

SELECT Id, Name
FROM Campaign
WHERE Status != 'draft'
AND Status != 'cancel'

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