+ Start a Discussion
ANAMIKA MONDAL 8ANAMIKA MONDAL 8 

Trying to populate opportunity owner's manager

Dear all,

I've wrote a trigger which populates Opportunity owner's manager on opportunity. But when saving any opty it is giving me error like 'Apex trigger PopulateOwnerManager caused an unexpected exception, contact your administrator: PopulateOwnerManager: execution of BeforeInsert caused by: System.ListException: List index out of bounds: 0: Trigger.PopulateOwnerManager: line 7, column 1'

PFB code:
 
trigger PopulateOwnerManager on Opportunity (before insert) {
    
  for(Opportunity op : Trigger.new)  
  {
  
  List<Opportunity>  ownr = [Select OwnerId from Opportunity where id = :op.id]; 
      System.debug('oppty ownerId :' + ownr[0].OwnerId);
  List<User> mngr = [Select ManagerId from User where id= :ownr[0].OwnerId];
      System.debug('oppty owner managerID :' + mngr[0].ManagerId);
  op.Owner_s_Manager__c = mngr[0].ManagerId;    
      
      
  }  
    
}

Please help me sorting this out. Thanks in advance.
Maharajan CMaharajan C
HI Anamika,

Please try the below code and let me know:

trigger PopulateOwnerManager on Opportunity (before insert,before update) {
    
  for(Opportunity op : Trigger.new)  
  {
  
  List<User> mngr = [Select ManagerId from User where id= :op.Ownerid];
      System.debug('oppty owner managerID :' + mngr[0].ManagerId);
      
      op.Owner_s_Manager__c = mngr[0].ManagerId;    
          
  }  
    
}

If it solve your choose this as best answer!!!

Thanks,
Raj
raz rraz r
Please find the below code with bulikification.

trigger PopulateOwnerManager on Opportunity (before update) {
    set<id>oppid=new set<id>();
  for(Opportunity op : Trigger.new)  
  {
  oppid.add(op.id);
  }
  List<Opportunity>  ownr = [Select OwnerId from Opportunity where id = :oppid]; 
    set<id>ownerids=new set<id>();
    for(Opportunity o:ownr){
        ownerids.add(o.ownerId);
    }
  List<User> mngr = [Select ManagerId from User where id= :ownerids];
    
  for(Opportunity op : Trigger.new)  {
      for(user u :mngr){
     op.Owner_manager__c = u.ManagerId;    
      }
  }   
    
    
}
Piyali ChowdhuryyPiyali Chowdhuryy
Hi Riaz,

Thank you for sharing this. Works fine and for multiple records!
Deepak Maheshwari 7Deepak Maheshwari 7

Hi Anamika,

 

As per the salesforce best practice, we should not use nested for loop.

 

Please find the trigger below with best practice:

 

 

trigger PopulateOwnerManager on Opportunity (before insert,before update) {
  set<id>ownerids=new set<id>();

  for(Opportunity op : Trigger.new)  
  {
  ownerids.add(op.ownerid);
  }
  
  List<User> mngr = [Select ManagerId from User where id= :ownerids];
  Map<Id,Id> opp_Map = new Map<Id,Id>();
   
  for(User us: mngr) 
  {  
      opp_Map.put(us.Id,us.ManagerId);
  }

  for (Opportunity opp:Trigger.new)
  {
      if(opp_Map!=null && opp_Map.containskey(opp.OwnerId))
      {  
          opp.Owner_s_Manager__c =opp_Map.get(opp.OwnerId);
      }    
  }
                
 }

ANAMIKA MONDAL 8ANAMIKA MONDAL 8
Hi Maharaja, It worked, but can you please explain me what was the problem previously, with the list of opty owners and then using it in the list of managers.

Hi Raj and Deepak, thanks for your assistance.
Maharajan CMaharajan C
Hi Anamika,

There is a no Seperate List to store and query the Inserted opp Ids here sothat i just removed that first List line and then i can get the Opp Ids from the Loop which have the list of opportunity.

Thanks,
Raj.
Maharajan CMaharajan C
Hi Anamika,

Choose the best answer !!!