2

I would like to convert the following trigger to before insert/update (so that I don't have to include an "update lead" statement), but I'm not sure how to get the new value of the lead owner field if I do so:

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 Answers 2

4

Do you need to check if the ownerId has changed on insert? Is there another trigger in play or a lead assignment rule that would be changing the owner ID?

Generally speaking, Trigger.oldMap isn't applicable for an insert trigger as there are no old values to check against. Instead you can use Trigger.isInsert to skip that test. See Trigger Context Variables

How about something like the following? It isn't as sexy without using the developer preview of the SOQL polymorphism feature, but it should get you close.

trigger SetLeadOwnerManager on Lead(before insert, before update)
{
    Map<Id, Id> ownerMap = new Map<Id, Id>();
    for (Lead lead : Trigger.new)
    {
        if(Trigger.isInsert || 
           (System.Trigger.oldMap.get(lead.Id).OwnerId != 
            lead.OwnerId)) {
            // Queue if a new Lead or the OwnerId has changed.
            ownerMap.put(lead.OwnerId, null);
        }
    }

    if (ownerMap.size() > 0)
    {
        for (User[] users : [SELECT Id, ManagerId FROM User WHERE Id IN :ownerMap.keySet()])
        {
            for (User user : users)
            {
                ownerMap.put(user.Id, user.ManagerId);
            }
        }
        for (Lead lead : Trigger.new)
        {
            // Should null be assigned here if the Owner isn't a user?

            // This assignment should probably be skipped if the lead id isn't in the 
            // ownerMap keyset or you will null out the values when there is no change.
            if(ownerMap.containsKey(lead.OwnerId)) {
                lead.Lead_Owner_Manager__c = ownerMap.get(lead.OwnerId);
            }
        }
    }
}
7
  • 1
    Don't be so hard on yourself, I still think its pretty sexy :) +1 May 12, 2014 at 21:58
  • 1
    @sfdc_ninja I think they should call the new feature ad hoc SOQL polymorphism for the extra computer science appeal. It has a very appealing ring to it IMHO. May 12, 2014 at 22:07
  • I agree, was just playing around. I agree with your assessment. May 13, 2014 at 10:44
  • Daniel, this seems to work great! Thank you! :-) Can you please expound on your comments near the end of the code? If the owner isn't a user, then null should be assigned (which it appears to be doing in actuality). May 14, 2014 at 14:22
  • 1
    @RobAlexander No problem. I've added a quick check to ensure the lead OwnerId is in the Maps keyset. This will prevent null being assigned if there is no change to the value. You might want to consider what the requirements are if the Owner isn't a user. May 14, 2014 at 22:29
1

You should not be requerying the records. You should be looping over Trigger.new and modifying those records:

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

for (Lead lead: leadList) {

Instead:

for (Lead aLead: Trigger.new){

EDIT: If you need that additional User data, Query separately for it and put it into a Map so that you can fetch it when needed.

2
  • Isn't my list limited to Trigger.new and isn't my query outside of my for loop? May 12, 2014 at 20:35
  • 1) Why query data you already have access to? 2) You need to update the values of Trigger.new in order to accomplish what you want.
    – dphil
    May 12, 2014 at 20:36

You must log in to answer this question.

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