Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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;

          }
        }
    }
share|improve this question

2 Answers 2

up vote 3 down vote accepted

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);
            }
        }
    }
}
share|improve this answer
1  
Don't be so hard on yourself, I still think its pretty sexy :) +1 –  sfdc_ninja May 12 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. –  Daniel Ballinger May 12 at 22:07
    
I agree, was just playing around. I agree with your assessment. –  sfdc_ninja May 13 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). –  RobAlexander May 14 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. –  Daniel Ballinger May 14 at 22:29

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.

share|improve this answer
    
Isn't my list limited to Trigger.new and isn't my query outside of my for loop? –  RobAlexander May 12 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 at 20:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.