You need to sign in to do that
Don't have an account?
ANAMIKA 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:
Please help me sorting this out. Thanks in advance.
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.
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
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;
}
}
}
Thank you for sharing this. Works fine and for multiple records!
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);
}
}
}
Hi Raj and Deepak, thanks for your assistance.
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.
Choose the best answer !!!