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 

can uplz execute this code?

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.... :)

 

ManojjenaManojjena
Hi Sai ,

Try with below code ,What I think you need to update the opportunity not insert and have some issue in query string which I have modified ,please check .
global class OpportunityBatch implements database.batchable<sObject>{ global String opptyList;
	global Database.QueryLocator start(Database.BatchableContext info){
		 String status = ‘Submitted’;
		 String opptyList = ‘SELECT name,AccountName__c FROM Opportunity WHERE status__c =\'submitted\'' ;
		 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);
		}
		try{
			 update opportunitiesList;
		}catch(DmlException de ){
			System.debug(de);
		}
	}
	global void finish(Database.batchableContext info){}
	 
}
Thanks
Manoj
 
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 object(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