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
Ayyagari RameshAyyagari Ramesh 

How to bulkify the apex code related to process builder

I have written an invocable apex class for process builder to update th owner change of accounts and related Opportunity, contracts and quotes on custom object and its working fine. Now i need to bulkify the code. what are the changes required in below code ?
below code :
public class RequestPortalRecords
{
    @InvocableMethod
    public static void ReqChange(List<Request_Portal__c> rp)
    {
        system.debug('entered raghav' +rp);
        Set<Id> accountIds=new Set<Id>();
        Id ownerid;
        list<Account> updateAccOwnerList = new list<Account>();
        list<Opportunity> updateoppList = new list<Opportunity>();
        list<SBQQ__Quote__c> updateqsList = new list<SBQQ__Quote__c>();
        list<contract> updatecrList=new list<contract>();
        
        
        for(Request_Portal__c r : rp)
        {
            accountIds.add(r.Account_Name__c);
            ownerid=r.New_Owner__c;
            system.debug('accountIds'+accountIds+' ownerid'+ ownerid);
            
        }
        
        //updaTE ACCOUNT
        List<Account> accList=[Select id,ownerid from account where id=:accountIds];
        system.debug('accList'+accList.size());
        
        for(Account acc:accList){
            acc.ownerid=ownerid;
            updateAccOwnerList.add(acc);
        }
        
        system.debug('updateAccOwnerList'+updateAccOwnerList.size());
        if(updateAccOwnerList.size()>0){
            update updateAccOwnerList;
        }
        
        //update opportunities
        List<Opportunity> oppList=[Select id,Ownerid,AccountId from Opportunity Where AccountId = :accountIds ];
        system.debug('opplist'+oppList.size());
        
        
        
        for(Opportunity op: opplist){
            op.ownerid=ownerid;
            updateoppList.add(op);
            
        }
        
        system.debug('updateoppList'+updateoppList);  
        
        if(!updateoppList.isEmpty())
            update updateoppList;  
    
    //update Quote
    List<SBQQ__Quote__c> qsList=[Select id,Ownerid from SBQQ__Quote__c where SBQQ__Account__c = :accountIds];
     for(SBQQ__Quote__c qs: qsList){
            qs.ownerid=ownerid;
           updateqsList.add(qs);
            
      }
        system.debug('updateqsList'+updateqsList);  
        
        if(!updateqsList.isEmpty())
            update updateqsList; 
        
        //contract
        List<Contract> crList=[Select id,Ownerid from Contract where AccountId = :accountIds];
     for(Contract qs: crList){
            qs.ownerid=ownerid;
           updatecrList.add(qs);
            
      }
        system.debug('updatecrList'+updatecrList);  
        
        if(!updatecrList.isEmpty())
            update updatecrList;  
    }    
}
Best Answer chosen by Ayyagari Ramesh
Tad Aalgaard 3Tad Aalgaard 3
I don't see any issues with bulkification.  However I was wondering about this code.
ownerid=r.New_Owner__c;
During the iteration it will keep overwriting the ownerid value with the New_Owner__c value from the next Request_Portal__c record.  After the iteration is done the value of ownerid will be the value of the last Request_Portal__c  read.  Is that what you wanted?  If no, then you need to create a map to store the key pair of r.Account_Name__c and r.New_Owner__c.  Even this approach might have an issue if there exists a Request_Portal__c  record with the same Acount_Name__c value and different New_Owner__c values as this would mean that only the last New_Owner___c for the same Account_Name__c would be saved in the map.
 

All Answers

Tad Aalgaard 3Tad Aalgaard 3
I don't see any issues with bulkification.  However I was wondering about this code.
ownerid=r.New_Owner__c;
During the iteration it will keep overwriting the ownerid value with the New_Owner__c value from the next Request_Portal__c record.  After the iteration is done the value of ownerid will be the value of the last Request_Portal__c  read.  Is that what you wanted?  If no, then you need to create a map to store the key pair of r.Account_Name__c and r.New_Owner__c.  Even this approach might have an issue if there exists a Request_Portal__c  record with the same Acount_Name__c value and different New_Owner__c values as this would mean that only the last New_Owner___c for the same Account_Name__c would be saved in the map.
 
This was selected as the best answer
Ayyagari RameshAyyagari Ramesh
@Tad Got you. Thanks