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
parth jollyparth jolly 

Error in batch class when i run the test class code

Hi Community ,

I have written the test class code for opportunity trigger and included the batch class test code in that But whenever i run it it showes error in batch class.
 
System.QueryException: unexpected token: AND

here is my test class code and batch class code .Please Check and let me know where i am doing wrong
 
global class ForecastDeleteBatch implements Database.Batchable<sObject>{      
    
    global ForecastDeleteBatch(){
    
    } 

    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator('SELECT Id,StageName,Name,Created_Date_Formula__c , Date_After_1_Month__c FROM Opportunity WHERE Name = \'ServiceDesk Online - Monthly Usage\' OR Name = \'ServiceDesk Online Purchase\' AND StageName = \'Closed Won\'');
    }

    global void execute(Database.BatchableContext BC, List<sObject> scope){   
    List<Opportunity> opplist = new List<Opportunity>();
    
    
        for(Opportunity opp: (List<Opportunity>)Scope){  
        
          if(opp.Name == 'ServiceDesk Online - Monthly Usage'||opp.Name == 'ServiceDesk Online Purchase' && opp.StageName == 'Closed Won'){
              if(opp.Date_After_1_Month__c == System.today()){  
               System.debug('number'+opplist);
                  opplist.add(opp);
              }
           }
        }
         
         System.debug('number of opp'+opplist);
         if(opplist.size() > 0){
             delete opplist;
             System.debug('number of opp updated'+opplist);
         }
          
    
    }
   
    global void finish(Database.BatchableContext BC){
              
    }   
}
 
@isTest
public class OpportunityTrigger_Test{
    static testMethod void insertNewOpportunity() {
        Account act = new Account();           
            act.Name = 'Test';
            act.Territory__c = 'Israel';
            //act.CurrencyIsoCode = 'USD -U.S. Dollar';
            act.Pipeline__c = 'Database';
     
        
        insert act;
        
      /*  Product2 prod = new Product2(Name = 'Laptop X200', 
                                     Family = 'Hardware',SKU__c = 'SD-Pro-Y');
        insert prod;
        
        Id pricebookId = Test.getStandardPricebookId();
        
        PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, Product2Id = prod.Id,
            UnitPrice = 10000, IsActive = true);
        insert standardPrice;
        
        
        Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
        insert customPB;
        
        PricebookEntry customPrice = new PricebookEntry(
            Pricebook2Id = customPB.Id, Product2Id = prod.Id,
            UnitPrice = 12000, IsActive = true);
        insert customPrice;
        */
        Opportunity opp= new Opportunity();
            opp.StageName = 'Responded to Buyer';
            opp.CloseDate = System.today();
            opp.Account = act;
            opp.Name = 'ServiceDesk Online - Monthly Usage' ; 
            
               
        insert opp;
        
    
        opp.StageName = 'Closed Won';

        Test.startTest();

        update opp;

            ForecastDeleteBatch btch = new ForecastDeleteBatch();
            Database.executeBatch(btch);

        Test.stopTest();
      
    }
}

 
FearNoneFearNone
the error means that it is confused where to AND ( (A OR B) AND C, A OR (B AND C) ).
just add parenthesis.
return Database.getQueryLocator('SELECT Id,StageName,Name,Created_Date_Formula__c , Date_After_1_Month__c FROM Opportunity WHERE (Name = \'ServiceDesk Online - Monthly Usage\' OR Name = \'ServiceDesk Online Purchase\') AND StageName = \'Closed Won\'');

 
Dushyant SonwarDushyant Sonwar
parth , @FearNone is right , you have created a malformed query in batch class
Above adding parenthesis will resolve your issue.

You can also use variable to use in dynamic query if you are having problem in adding single quotes in query.
Just adding more detail in FearNone answer :)
 
String monthlyUsage = 'ServiceDesk Online - Monthly Usage'; 
String purchase = 'ServiceDesk Online Purchase'; 
String stageName = 'Closed Won'; 

return Database.getQueryLocator('SELECT Id,StageName,Name,Created_Date_Formula__c , Date_After_1_Month__c FROM Opportunity WHERE (Name = :monthlyUsage OR Name = :purchase ) AND StageName = :stageName ');