2

I have a situation where I want a certain type of Task to trigger an email alert to the "Who" Contact or Lead that the task is related to. In the Spring '16 release Salesforce added the ability to send Email Alerts on Task workflows, which is great, but I can't seem to make it send to the related Lead or Contact - they don't come up as options for the Recipient.

Has anyone found a workaround to this?

  • And Related Contact or Lead Owner which is a choice in the recipient drop down doesn't work? – cropredy Mar 22 '16 at 19:44
  • No - I don't want to send email to the related Lead or Contact Owner, but to the related Lead or Contact themselves. – OBerm Mar 23 '16 at 14:38
2

Try this:

  1. Create a custom Activity field Email__c of type Email
  2. Populate it with the trigger below
  3. You now have an email field available to the Email Alert

    trigger TaskTrigger on Task (before insert, before update) {
      set<ID> lIdSet = new set<ID>();
      set<ID> cIdSet = new set<ID>();
      for (Task t : Trigger.new)  // collect parentIds
         if (t.whoId == null) {}
         else
         if (t.whoId.getSobjectType() == Lead.SObjectType)
            lIdSet.add(t.whoId);
         else
         if (t.whoId.getSobjectType() == Contact.SObjectType)
            cIdSet.add(t.whoId);
    
      // Find the parents
      map<ID,Lead> lIdToLeadMap = new map<ID,Lead>([select id, email 
                                                        from Lead where id IN :lIdSet]);
      map<ID,Contact> cIdToContactMap = new map<ID,Contact>([select id, email 
                                                        from Contact where id IN :cIdSet]);
    
      // now populate the custom email__c field from parent
      for (Task t: Trigger.new) 
        t.email__c = lIdToLeadMap.containsKey(t.whoId)
           ? lIdToLeadMap.get(t.whoId).email
           : cIdToContactMap.containsKey(t.whoId)
               ? cIdToContactMap.get(t.whoId).email
               : null; 
    
      // since this is a before trigger, changing value of Trigger.new members implicitly updates the database
    }
    
| improve this answer | |
  • Brilliant, I will try this and let you know how it goes. – OBerm Mar 23 '16 at 16:23
  • Worked like a charm, thanks for the great advice and working code! – OBerm Mar 24 '16 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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