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
MaheemSamMaheemSam 

Change code to bulkified

Hi, 

  I have a helper class code which is working as expected need your suggestion in making this bulkified. Please suggest the code change with logic
public static void processOptySharingOppTeam(List<Opportunity> newLst) {
     List<id> actIds = new List<id>();
     List<id> paractIds = new List<id>();
     List<id> OpptIds = new List<id>();
     map<id,Opportunity_Sharing__c> oppshare = new map<id,Opportunity_Sharing__c>([select id, Account_Names__c, Key_Text__c, Partner_Account_Names__c,User__c,Access_Level__c,Global_Account_Rep__c,Top_Account_ID__r.id, Top_Partner_Account_ID__r.id from Opportunity_Sharing__c]);
     List<OpportunityTeamMember> Otmlst = new List<OpportunityTeamMember>();
     String TopActID;
     String TopPartActId; 
     
     for(Opportunity opp : newLst) {
       OpptIds.add(opp.id); 
       actIds.add(opp.AccountID);
       paractIds.add(opp.partner_account__c);
         }
  
       if(!actIds.isempty()){
       TopActID = GetTopLevleElement(actIds[0]);
       }
       
       if(!paractIds.isempty()){
       TopPartActId = GetTopLevleElement(paractIds[0]);
       }
      
        system.debug('TopActID  :' + TopActID);
        system.debug('TopPartActId :' + TopPartActId); 
      
   
       for(Opportunity_Sharing__c rec : oppshare.values()){
            if(TopActID <> null && TopActID == rec.Top_Account_ID__r.id){
              system.debug('Found Account Id '  + rec.Top_Account_ID__r.id );
               if( rec.User__c != null & rec.Access_Level__c != null ){
                   OpportunityTeamMember otm = new OpportunityTeamMember(OpportunityId = OpptIds[0],UserId=rec.User__c,OpportunityAccessLevel=rec.Access_Level__c
                                                                              ,TeamMemberRole=rec.Global_Account_Rep__c  ); 
                        Otmlst.add(otm);
                 }
               }      
               
            if(TopPartActId <> null && TopPartActId  == rec.Top_Partner_Account_ID__r.id){
              system.debug('Found Partner Account Id '  + rec.Top_Partner_Account_ID__r.id );
               if( rec.User__c != null & rec.Access_Level__c != null ){
                   OpportunityTeamMember otm = new OpportunityTeamMember(OpportunityId = OpptIds[0],UserId=rec.User__c,OpportunityAccessLevel=rec.Access_Level__c
                                                                              ,TeamMemberRole=rec.Global_Account_Rep__c  ); 
                        Otmlst.add(otm);
                 }
               }      
               
               
           }
          
            if(!Otmlst.Isempty()){ 
              insert Otmlst; 

       }   
   
   }
            
    public static String GetTopLevleElement(Id objId ){
        Boolean topLevelParent = false;
        if( objId <> null){
        while ( !topLevelParent ) {
            Account a = [ Select Id, ParentId From Account where Id = :objId limit 1 ];
            if ( a.ParentID != null ) {
                objId = a.ParentID;
            }
            else {
                topLevelParent = true;
            }
        }
         }
        return objId ;
    }

Thanks
Sudhir
 
Leonardi KohLeonardi Koh
The code itself appears to have the bulk treatment for it's DML so the main issue is the GetTopLevleElement which is performing an SOQL query in a loop, this is the one that's going to cause a problem.

It looks like the method is made to trace and find the top level parent Account, so we'll have to think of a way to replace this method.