5

Why isn't this code writing the lead owner's manager to the Lead_Owner_Manager__c field?

if(Trigger.isUpdate && System.Trigger.oldMap.get(x.Id).OwnerId != x.OwnerId){
        dh.Old_Value__c = System.Trigger.oldMap.get(x.Id).OwnerId ;
        DHs.put(x.id,dh);
        /* Note that we have to assign the polymorphic reference to the appropriate sObject type before passing to a method*/
        if (x.Owner instanceof User) {
            User userOwner = x.Owner;
            x.Lead_Owner_Manager__c = userOwner.ManagerId;
        }
}

UPDATE: I updated my code to the following, but it still doesn't work, even though it compiles and saves:

//set Lead Owner's Manager field

List<Lead> leadList = [SELECT TYPEOF Owner WHEN User THEN ManagerId END FROM Lead WHERE Id IN : Trigger.new];   

    for (Lead lead: leadList) {

        if (lead.Owner instanceof User) {
            User userOwner = lead.Owner;
        if(System.Trigger.oldMap.get(lead.Id).OwnerId != lead.OwnerId){
            lead.Lead_Owner_Manager__c = userOwner.ManagerId;
            }
        }   
    }

3 Answers 3

6

The issue with above code is you cannot access relationship field directly through trigger context fields .

So a small query will help you to retrieve manager Id

if(Trigger.isUpdate && System.Trigger.oldMap.get(x.Id).OwnerId != x.OwnerId){
    dh.Old_Value__c = System.Trigger.oldMap.get(x.Id).OwnerId ;
    DHs.put(x.id,dh);
    /* Note that we have to assign the polymorphic reference to the appropriate
       sObject type before passing to a method*/
    if (x.Owner instanceof User) {
        SetUserIds.add(dh.OwnerId);//collect in Set
    }

   Map<Id,Id> mapUserIdByManagerId=new Map<Id,Id>();

     for(User u:select Id,ManagerId from User where Id in:SetUserIds){
            mapUserIdByManagerId.put(u.Id,U.ManagerId);
    }

     x.Lead_Owner_Manager__c = mapUserIdByManagerId.get(x.OwnerId);

}

Not sure how your code is structured but remove all queries outside for loop.

0
2

If you need to grab fields via a relationship, you need to query for the fields from the relationships to actually get the value. You can then use a Map to assign the associated values.

trigger SetLeadOwnerManager on Lead(before insert, before update)
{
  Set<Id> ownerIds = new Set<Id>();
  for (Lead curr: Trigger.new)
  {
    //Getting Set of all OwnerIds
    ownerIds.add(curr.OwnerId);
  }

  //This is the relationship I was explaining here.
  Map<Id, User> ownerToUserMap = new Map<Id, User>([SELECT ManagerId FROM User WHERE Id IN :userIds]);
  for (Lead curr: Trigger.New)
  {
    if (Trigger.isUpdate)
      curr.Old_Value__c = TriggerOldMap.get(curr.Id).OwnerId;

    if (Trigger.isInsert || Trigger.oldMap.get(curr.Id).OwnerId != curr.OwnerId)
    {
      User aUser = ownerToUserMap.get(curr.ownerId);
      if (aUser != null)
      {
        curr.Leader_Owner_Manager__c = aUser.ManagerId;
      }
    }
  }
}
-1

I ended up putting the following code in a new trigger, which works:

trigger SetLeadOwnerManager on Lead (after insert, after update) {

List<lead> leadList = [SELECT TYPEOF Owner WHEN User THEN ManagerId END FROM Lead WHERE Id IN : Trigger.new];   
    system.debug('leadList:  ' + leadList);
    for (Lead lead: leadList) {

        if(System.Trigger.oldMap.get(lead.Id).OwnerId != System.Trigger.newMap.get(lead.Id).OwnerId){

            if (lead.Owner instanceof User) {
            User userOwner = lead.Owner;
            lead.Lead_Owner_Manager__c = userOwner.ManagerId;
                update lead;

            }
        }
    }

}

2
  • 1
    You should be doing this in a before trigger so you don't have to double update your leads. As you have it now, you are going to hit every lead trigger twice instead of once. Mohith's solution is much more thorough, although he does add an extra unneccessary loop.
    – dphil
    Commented May 7, 2014 at 19:55
  • I tried it in a before trigger and was getting the old owner instead of the new owner. Commented May 7, 2014 at 21:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .