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
Pavani Akella 9Pavani Akella 9 

How to write NOT operator in an apex trigger SOQL query

I would like to add a clause in the query where status not equal to "Approved or Closed or Denied". How do I add that logic in this query
List<Case> cas=Database.query('SELECT Id,CaseNumber FROM Case WHERE AccountId =\''+sc+'\' AND VIN_Entry__c =\''+ event.VIN__c+'\'');

Thanks in advance!
 
Yash VoraYash Vora
Hi,
I think this should work fine.

List<Case> cas=Database.query('SELECT Id,CaseNumber FROM Case WHERE AccountId =\''+sc+'\' AND VIN_Entry__c =\''+ event.VIN__c+'\ AND Status!='Approved ' AND Status!='Closed' AND Status!='Denied'');
Pavani Akella 9Pavani Akella 9
HI Yash,

Thanks fir your response. I tried the llogic but it throws me errors 
Yash VoraYash Vora
Can you post the error what you are getting
Khan AnasKhan Anas (Salesforce Developers) 
Hi Pavani,

I trust you are doing very well.

Please use below approach:
 
String[] filter = new String[]{'Approved','Closed','Denied'};
List<Case> cas=Database.query('SELECT Id,CaseNumber FROM Case WHERE AccountId =\''+sc+'\' AND VIN_Entry__c =\''+ event.VIN__c+'\' AND Status NOT IN: filter');

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas
Pavani Akella 9Pavani Akella 9
Thank you Khan for the repsonse. I'm getting an error when I keep the status not in filter before event.VIN__c but now the problem is if the status clause is after the evnt. VIN__C the apex trigger is not returning right results
Pavani Akella 9Pavani Akella 9
User-added image

Here is the error message for you to better understand. Thanks!
Raj VakatiRaj Vakati
Try this and its wokring for me
 


String s11 = 'SELECT Id,CaseNumber FROM Case WHERE AccountId =\''+event.VIN__c+'\' AND VIN_Entry__c =\''+ sc+'\' AND Status!=\'Approved\''+
    '\' AND Status!=\'Closed\''+'\' AND Status!=\'Denied\'';
System.debug(s11);


List<Case> cas=Database.query(s11 );

 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Pavani,

You are misplacing single quotes. Just copy paste below line of code. I have tested in my org and it is working fine.
 
String[] filter = new String[]{'Approved','Closed','Denied'};
List<Case> cas=Database.query('SELECT Id,CaseNumber FROM Case WHERE AccountId =\''+sc+'\' AND VIN_Entry__c =\''+ event.VIN__c+'\' AND Status NOT IN: filter');

Regards,
Khan Anas