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
cool_codingcool_coding 

Unexpected token: OR in batch

Hello friends,

I am writing a batch and in the start method I am having this error "Unexpected token: OR in batch",
in my query. Can anyone please help me
Below is my piece of code. Thanks

The error is here  query += ' OR Statut__c = \'Caduque\'';
 
global class Batch_EnvoieMailContratPerdu implements Database.Batchable<sObject>{
    
    global Database.QueryLocator start(Database.BatchableContext BC){
         String query = null;
        
        query = 'SELECT Id';
        query += ', Reference_proposition_commerciale__c, Statut__c';
        query += ', ContratPerdue__c';
        query += ' FROM Contrat__c';
        query += ' WHERE ContratPerdue__c = false';
        query += ' AND Reference_proposition_commerciale__c != null';
        query += ' AND Statut__c = \'Résilié\'';
        query += ' OR Statut__c = \'Caduque\'';
       
         System.debug(query);
         return Database.getQueryLocator(query);
    }


 
Best Answer chosen by cool_coding
BALAJI CHBALAJI CH
Hi,
When we are using logic operators on same field, we can use parentheses to define the order in which fieldExpressions are evaluated.
Please find below modified code:
global class Batch_EnvoieMailContratPerdu implements Database.Batchable<sObject>{
    
    global Database.QueryLocator start(Database.BatchableContext BC){
         String query = null;
        
        query = 'SELECT Id';
        query += ', Reference_proposition_commerciale__c, Statut__c';
        query += ', ContratPerdue__c';
        query += ' FROM Contrat__c';
        query += ' WHERE ContratPerdue__c = false';
        query += ' AND Reference_proposition_commerciale__c != null';
        query += ' AND (Statut__c = \'Résilié\'';
        query += ' OR Statut__c = \'Caduque\')';
       
         System.debug(query);
         return Database.getQueryLocator(query);
    }
Please let me know if that helps you.

Best Regards,
BALAJI
 

All Answers

BALAJI CHBALAJI CH
Hi,
When we are using logic operators on same field, we can use parentheses to define the order in which fieldExpressions are evaluated.
Please find below modified code:
global class Batch_EnvoieMailContratPerdu implements Database.Batchable<sObject>{
    
    global Database.QueryLocator start(Database.BatchableContext BC){
         String query = null;
        
        query = 'SELECT Id';
        query += ', Reference_proposition_commerciale__c, Statut__c';
        query += ', ContratPerdue__c';
        query += ' FROM Contrat__c';
        query += ' WHERE ContratPerdue__c = false';
        query += ' AND Reference_proposition_commerciale__c != null';
        query += ' AND (Statut__c = \'Résilié\'';
        query += ' OR Statut__c = \'Caduque\')';
       
         System.debug(query);
         return Database.getQueryLocator(query);
    }
Please let me know if that helps you.

Best Regards,
BALAJI
 
This was selected as the best answer
Snehal Pathre 8Snehal Pathre 8
Query should be formed as follows:

global class Batch_EnvoieMailContratPerdu implements Database.Batchable<sObject>{

global Database.QueryLocator start(Database.BatchableContext BC){
query = null;

query = 'SELECT Id';
query += ', Reference_proposition_commerciale__c, Statut__c';
query += ', ContratPerdue__c';
query += ' FROM Contrat__c';
query += ' WHERE ContratPerdue__c = false';
query += ' AND Reference_proposition_commerciale__c != null';
query += ' AND (Statut__c = \'Résilié\'';
query += ' OR Statut__c = \'Caduque\')';

System.debug(query);
return Database.getQueryLocator(query);

conditions must specify in parentheses when we are using multiple operators of different types. However, multiple operators of the same type do not need to be nested.