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
sai kumar 433sai 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){
 
 
}
 
JeeTJeeT
Hi Sai,

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.... :)
NagendraNagendra (Salesforce Developers) 
Hi Sai Kumar 433,

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