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
MaggieSumitMaggieSumit 

How to Update acccount owner and opportunity Stage, If opportunity stage equal to Qualification by Batch Class

This Code but I think this is not correct
  1. global class BatchOpportunitys implements Database.Batchable<SObject>, Database.Stateful{
  2.              
  3.     
  4.     List<Opportunity> listRecords = new List<Opportunity>();
  5.    
  6.     global Database.QueryLocator start(Database.BatchableContext BC)
  7.     {
  8.         String query = 'Select Id, Name, CloseDate,StageName From Opportunity';
  9.         return Database.getQueryLocator(query);
  10.     }
  11.      
  12.     global void execute(Database.BatchableContext BC, List<SObject> scope){
  13.     
  14.         for(Opportunity obj : (Opportunity[]) scope){
  15.             
  16.             if(obj.StageName == 'Qualification'){
  17.                 obj.CloseDate = date.today();
  18.                 obj.StageName = 'Value Proposition';
  19.                 obj.account.ownerId = obj.ownerId;
  20.                 obj.account.Status__c = 'Lost';   
  21.                 listRecords.add(obj);
  22.             }
  23.             
  24.          }
  25.     
  26.     }
  27.     
  28.     global void finish(Database.BatchableContext BC){
  29.         system.debug('list size  :: '+listRecords.size());
  30.         if(!listRecords.isEmpty())
  31.             {
  32.               update listRecords;
  33.             }
  34.     }
  35. }

 
Raj VakatiRaj Vakati
Hi Sumit ,
here is the code  .

global class BatchOpportunitys implements Database.Batchable<SObject>, Database.Stateful{
    
    
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'Select Id, Name,AccountId, CloseDate,StageName From Opportunity';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<SObject> scope){
        List<Opportunity> opptobeUpdate = new List<Opportunity>();
        List<Account> accobeUpdate = new List<Account>();
        
        for(Opportunity obj : (Opportunity[]) scope){
            if(obj.StageName == 'Qualification'){
                obj.CloseDate = date.today();
                obj.StageName = 'Value Proposition';
                Account a = obj.Account; 
                a.ownerId = obj.ownerId ;
                
                //obj.account.ownerId = obj.ownerId;
                //   obj.account.Status__c = 'Lost';   
                opptobeUpdate.add(obj);
                accobeUpdate.add(a) ;
            }
            
        }
        if(opptobeUpdate.size()>0){
            update opptobeUpdate ;
        }
        if(accobeUpdate.size()>0){
            update accobeUpdate ;
        }
        
    }
    
    global void finish(Database.BatchableContext BC){
    }
}
 
MaggieSumitMaggieSumit
Thanks @rajamohan vakati 17 But getting error User-added image
Raj VakatiRaj Vakati
Use this code


global class BatchOpportunitys implements Database.Batchable<SObject>, Database.Stateful{
    
    
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'Select Id, Name,AccountId, CloseDate,StageName From Opportunity';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<SObject> scope){
        List<Opportunity> opptobeUpdate = new List<Opportunity>();
        List<Account> accobeUpdate = new List<Account>();
        
        for(Opportunity obj : (Opportunity[]) scope){
            if(obj.StageName == 'Qualification'){
                obj.CloseDate = date.today();
                obj.StageName = 'Value Proposition';
                if(obj.Account!=null){
                    Account a = obj.Account; 
                    a.ownerId = obj.ownerId ;
                    accobeUpdate.add(a) ;
                    obj.account.Status__c = 'Lost';   
                    
                }
                
                //obj.account.ownerId = obj.ownerId;
                //   obj.account.Status__c = 'Lost';   
                opptobeUpdate.add(obj);
            }
            
        }
        if(opptobeUpdate.size()>0){
            update opptobeUpdate ;
        }
        if(accobeUpdate.size()>0){
            update accobeUpdate ;
        }
        
    }
    
    global void finish(Database.BatchableContext BC){
    }
}
 
MaggieSumitMaggieSumit
Thanks @rajamohan vakati 17 But its only updating opportunity and Not updating Account fields