You need to sign in to do that
Don't have an account?
sai kumar 433
plz execut this code errors getting
global class OpportunityBatch implements database.batchable<sObject>{ global String opptyList;
global Database.QueryLocator start(Database.BatchableContext info){ String status = ‘Submitted’;
List<Opportunity> opptyList = ‘select name,AccountName__c from Opportunity where status__c =\”+ status +’\” ;
return Database.getQueryLocator(opptyList);
}
global void execute(Database.batchableContext info,List<Opportunity> opptyList){ List<Opportunity> opportunitiesList = new List< Opportunity >();
for(Opportunity oppty: opptyList){ oppty.status__c = ‘Approved’; opportunitiesList.add(oppty);
}
Insert opportunitiesList;
}
global void finish(Database.batchableContext info){
}
global Database.QueryLocator start(Database.BatchableContext info){ String status = ‘Submitted’;
List<Opportunity> opptyList = ‘select name,AccountName__c from Opportunity where status__c =\”+ status +’\” ;
return Database.getQueryLocator(opptyList);
}
global void execute(Database.batchableContext info,List<Opportunity> opptyList){ List<Opportunity> opportunitiesList = new List< Opportunity >();
for(Opportunity oppty: opptyList){ oppty.status__c = ‘Approved’; opportunitiesList.add(oppty);
}
Insert opportunitiesList;
}
global void finish(Database.batchableContext info){
}
Hope you are getting comile time error for this code..
1- You have a silly mistake in the code that you are trying to initialize a string to list of opportunity.Make a corredction to the line: List<Opportunity> opptyList = ‘select name,AccountName__c from Opportunity where status__c =\”+ status +’\” ;
to String opptyList = ‘select name,AccountName__c from Opportunity where status__c =\”+ status +’\” ;
Hope this helped.... :)
Please check the below code snippet for your requirement which works perfectly in my org. Try to create some custom fields on the respective sobject as mentioned in the code(Account name and status) and then try the below code.
Batch Class :
global class OpportunityBatch1 implements database.batchable<sObject>{ global Database.QueryLocator start(Database.BatchableContext info){ String status1 = 'Submitted'; // List<Opportunity> opptyList = 'select name,AccountName__c from Opportunity where status__c =:status1'; string opptyList = 'select name,AccountName__c,status__c from customer__c where status__c =:status1'; system.debug('opptylist is====================='+database.query(opptyList)); return Database.getQueryLocator(opptyList); } global void execute(Database.batchableContext info,List<Customer__c> opptyList){ List<Customer__c> opportunitiesList = new List<customer__c>(); for(customer__c oppty: opptyList){ oppty.status__c ='Approved'; opportunitiesList.add(oppty); system.debug('opportunitiesList========='+opportunitiesList); } update opportunitiesList; } global void finish(Database.batchableContext info){ } }
How to call the above batch class :
OpportunityBatch1 obj1=new OpportunityBatch1(); database.executeBatch(obj1);
Please mark my solution as the best answer if helps you...........
Best Regards,
Nagendra.P