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 

Bulkify the code

Hi,
  
  Below code works as expected by passing account id it will return the top account id only issue I see in below code is query is inside the while loop is there any other way to fix this issue and make the code bulkify.
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
  
Manohar kumarManohar kumar

Hi Sudhir, 
I don't think your code needs to be bulkified. Because your while loop will be called only once. so you are good. 

pls let me know you are calling this method in another loop. 

Thanks,
Manohar 

MaheemSamMaheemSam
Hi Manohar, 

      Below is the helper class how am using the method calling the class inside the trigger.  please the code comment I am calling the method. 
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; 
     
     system.debug('Opportunity List ' + newLst);
     
     for(Opportunity opp : newLst) {
       OpptIds.add(opp.id); 
       actIds.add(opp.AccountID);
       paractIds.add(opp.partner_account__c);
         }
  
     system.debug('opportunity id ' + OpptIds);
   
       if(!actIds.isempty()){
       TopActID = GetTopLevleElement(actIds[0]); // Here is the method am calling
       }
       
       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='Global Account Shared Owner'  ); 
                        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='Global Account Shared Owner' ); 
                        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
 
Manohar kumarManohar kumar

Hi Sudhir, 

Is this code working for you? If bilkification is the problem then you don't have to worry about that. Because its not in loop..
if its not, then you can try something like this. 

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>();
	 
	for(Opportunity opp : newLst) {
       OpptIds.add(opp.id); 
       actIds.add(opp.AccountID);
       paractIds.add(opp.partner_account__c);
	}
  
	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 where AccountID IN :actIds]);
	List<OpportunityTeamMember> Otmlst = new List<OpportunityTeamMember>();
	
     
	map<id, accountId> accMap = [Select Id, ParentId From Account where Id  IN :actIds]; 
	system.debug('Opportunity List ' + newLst);
    map<id, id> IdsVSIds = GetTopLevleElement(accMap); 
    system.debug('IdsVSIds::'+IdsVSIds); 
	system.debug('opportunity id ' + OpptIds);
   
       for(Opportunity_Sharing__c rec : oppshare.values()){
			if(IdsVSIds.contains(rec.AccountId)) {
				if(IdsVSIds.get(rec.AccountId) == 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='Global Account Shared Owner'  ); 
							Otmlst.add(otm);
					}
				}      
               
				if(IdsVSIds.get(rec.AccountId) == 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='Global Account Shared Owner' ); 
							Otmlst.add(otm);
					 }
				   }                                 
           }
          
            if(!Otmlst.Isempty()){ 
              insert Otmlst; 
       }     
	}
            
    public static map<id,id> GetTopLevleElement(map<Id, Account> accMap ){
        map<id, id> childVsParentId = new map<id, id>();
		for(account acc:accMap.values()) {
			if(acc.ParentID !=null) {
				childVsParentId.put(acc.id, acc.ParentID);
			}
			else {
				childVsParentId.put(acc.id, acc.id);
			}
		}
		return childVsParentId;
    }
 

if this doesn't help please let me know more about the condtions when you are trying to create opportunity team member. 

Thanks,
Manohar

MaheemSamMaheemSam
Thanks Manohar in my code I am actually calling two times TopActID  and TopPartActId 

I tried executing your code in line 48 of your code it is giving me Error: Compile Error: Unexpected token 'public'. at line 201 column 5