2

I want to send one email template to all contacts of an account automatically when a field update in Certification Object which is related to that account. Using workflow i couldn't find like all contacts of an account.

6
  • you may require trigger for this job..
    – Ratan Paul
    Feb 20, 2016 at 7:31
  • I don't think it is possible because you are dealing with 3 objects here.
    – Ratan Paul
    Feb 20, 2016 at 7:58
  • You can do this with a Flow and Process Builder. I think I have a Flow Plugin I wrote that could probably be modified slightly to send your email. I'm assuming this is a normal email template and not a visualforce one, right? Feb 20, 2016 at 17:46
  • What is the object you have attached to the merge fields in the email template? Is it a Contact email template or an Account template? Feb 20, 2016 at 18:05
  • I have a plugin you can use that will do this. I'll post it up when I get home later. Feb 20, 2016 at 19:14

2 Answers 2

4

So my Flow Plugin was a bit overkill and stripping it down was going to be too annoying, so I'm going to answer with a simple Invokable Method that I had lying around that should do the trick for sending the emails to all contacts passed related to an Account. If you want to filter the contacts, modify the contactsToEmail variable with whatever conditions you want. Additionally, make sure you edit the templateId query with the name of your email template.

Honestly, not the greatest way to do this, and doesn't include any error handling on the sendEmail result, but it works and can easily be touched up to make it work however you want.

Simply select this as an Apex Action in Process Builder that fires off on your Certification object and just pass in the Certification record AccountId as the acc variable.

 public class SendEmailTemplatesToAccountContacts {

  @InvocableMethod
  public static void sendEmail(List<Account> acc) {
    String sourceAccountId = String.valueOf(acc[0].Id);
    List<Messaging.SingleEmailMessage> emailsToSend = new List<Messaging.SingleEmailMessage>();
    List<Contact> contactsToEmail = [SELECT Id
                                     FROM   Contact
                                     WHERE  AccountId = :sourceAccountId];
    Id templateId = [SELECT Id 
                     FROM   EmailTemplate 
                     WHERE  Name = 'Test Send Email'].Id;

    for(Contact con: contactsToEmail ){

      Messaging.SingleEmailMessage email = new messaging.SingleEmailMessage();
      email.setTemplateId(templateId);
      email.setTargetObjectId(con.Id);
      email.setSaveAsActivity(false); //or true if you want to save as an activity
      email.setSenderDisplayName('Sender Display Name'); //Here if you want to change the sender name
      emailsToSend.add(email);

    }
    Messaging.sendEmail(emailsToSend);
  }
}
0

You can go ahead with process builder having the criteria of field update(related to account) on certification object and in Immediate action you should have the @Visualflow with FAST LOOKUP to have all the contacts on that accountID and FAST UPDATE

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