You need to sign in to do that
Don't have an account?
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.
here is my test class code and batch class code .Please Check and let me know where i am doing wrong
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(); } }
just add parenthesis.
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 :)