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
raj jordanraj jordan 

batch class to update account related opportunities

Hi all,
when account have some related opprtunities i want to update opportunity field when account is updated it should get updated for one related opportunity or more than one using batch class 
Can you help me through this

Thanks in Advance
Jordan
Deepak Kumar SharmaDeepak Kumar Sharma
Hi Raj,
Just Call Database.executeBatch(new BatchOppUpdate(contactsToUpdate)); in Trigger on update event then define a batch class as follows:-

global class BatchOppUpdate implements Database.Batchable<sObject>{

List<Account> parentAccount = new List<Account>(); 
List<Opportunity> Opplist1 = new List<Opportunity>();

 global BatchOppUpdate(List<Account> AccountUpdate) {
      parentAccount = AccountUpdate; 
 }

global Database.QueryLocator start(Database.BatchableContext BC) { 
    return DataBase.getQueryLocator([SELECT Id, AccountID FROM Opportunity WHERE AccountID IN :parentAccount ]); 


global void execute(Database.BatchableContext BC , List <Opportunity> Opplist) { 
    for (Account acct : mapAccount){ 
        for (Opportunity opp : contactList){ 
             if (opp.AccountID == acct.Id){ 
                 Opplist1.add(new Opportunity( Id = opp.Id, ); 
             } 
         } 
    } 
update Opplist1; 

global void finish(Database.BatchableContext BC){ 

}


If It really helps you please give it as best answere :)