+ Start a Discussion
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

Please Help - Which Email trigger is better

Hey there,

I have ended up with two email triggers and I thought I would post on the forums to get help in deciding which one i should use.

Should it perhaps be one or the other or maybe even both?

I was also hoping I could get some advice on anyway that I may better the triggers. Thank you for your time:

trigger SendNewSerEmail on Service__c (after update) {
    
   
   
 //Get the correct email template here

    
List<EmailTemplate > EmailTemplate = [SELECT Id, Name FROM EmailTemplate WHERE Name = 'Destiny Survey_6Months' or Name = 'Destiny Survey_3Months' or Name = 'Destiny Survey_9Months']; 
Map<String,EmailTemplate > EmailMap = new Map<String,EmailTemplate >();
for(EmailTemplate email: EmailTemplate ){
  EmailMap.put(email.name, email);
}



    
    Set<Id> accountIds = new Set<Id>();
    for (Service__c Ser : trigger.new) {
        accountIds.add(Ser.Account__c);
    
}

    List<Account> accounts = [SELECT Id, (SELECT Id, Email FROM Contacts) FROM Account WHERE Id IN :accountIds];
    Map<Id, List<Contact>> accountMap = new Map<Id, List<Contact>>();
    for (Account acct : accounts) {
        List<Contact> contacts = new List<Contact>();
        for (Contact c : acct.Contacts) {
            contacts.add(c);
        }
        accountMap.put(acct.Id, contacts);
    }

    //It's best to make a list of emails to send, so that you can send them all at once at the end, in order to avoid hitting governor limits
    List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
    
   for (Service__c Ser : trigger.new) {
If(Ser.Last_Email_Sent__c != 'No Emails Sent'){
         for (Contact c : accountMap.get(Ser.Account__c)) {
         if (c.Email != null) {
         
            Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();
          If(Ser.Last_Email_Sent__c == '3 Month Support Survey (Adv)'||Ser.Last_Email_Sent__c == '3 Month Support Survey (IPC)'||Ser.Last_Email_Sent__c == '3 Month Support Survey (MOM)'){  
            EmailTemplate email = EmailMap.get('Destiny Survey_3Months');
            m.setTemplateId(email.Id);
            }
          else if(Ser.Last_Email_Sent__c == '6 Month Support Survey (Adv)'||Ser.Last_Email_Sent__c == '6 Month Support Survey (IPC)'||Ser.Last_Email_Sent__c == '6 Month Support Survey (MOM)')
           {  
            EmailTemplate email = EmailMap.get('Destiny Survey_6Months');
            m.setTemplateId(email.Id);
            }
            else if(Ser.Last_Email_Sent__c == '9 Month Support Survey (Adv)'||Ser.Last_Email_Sent__c == '9 Month Support Survey (IPC)'||Ser.Last_Email_Sent__c == '9 Month Support Survey (MOM)')
           {  
            EmailTemplate email = EmailMap.get('Destiny Survey_9Months');
            m.setTemplateId(email.Id);
            }
            m.setTargetObjectId(c.Id);
            m.setWhatId(Ser.Id);
            messages.add(m);   
        }
    }
    }
   }
    Messaging.sendEmail(messages);
    
}

trigger SendEmailtocontact on Service__c (after Update)
{
    List<String> lcontactEmails = new List<String>();
    Set<Id> sIds = new Set<Id>();
//Added
    Set<ID> accIds = new Set<ID>();
    for(Service__c qItr : Trigger.new)
{
        if(Trigger.isUpdate)
  {
            if(Trigger.oldmap.get(qItr.id).Service_Stage__c != qItr.Service_Stage__c && qItr.Service_Stage__c.containsIgnoreCase('Stage 4'))
   {
                sIds.add(qItr.id);
    //Added
    accIds.add(qItr.Account__c);
            }
        }
  //Will Never Execute as trigger will fire only if a record is updated
        else if(Trigger.isInsert)
  {
            if(Trigger.newMap.get(qItr.id).Service_Stage__c.containsIgnoreCase('Stage 4'))
   {
                sIds.add(qItr.id);
            }
        }
    }
// Modified
    //for(Account accItr : [SELECT id,(SELECT id FROM Contacts) FROM Account WHERE id IN (SELECT Account__c FROM Service__c WHERE Id IN: sIds)])
for(Account accItr : [SELECT id,(SELECT id FROM Contacts) FROM Account WHERE id IN : accIds])
{
        for(Contact con : accItr.contacts)
  {
            if(!String.isBlank(con.id))
   {
                lcontactEmails.add(con.id);
            }
        }
       // No need to put this condition here.
  /*if(!lcontactEmails.isEmpty())
  {
   EmailHandler.sendEmail(lcontactEmails);
  }*/   
    }
//Put this here
if(!lcontactEmails.isEmpty())
{
for(Service__c serv : Trigger.new)
{
if(Serv.Service_Name__c == 'Advantage Program')
{
        EmailHandler.sendADVEmail(lcontactEmails);
    }
    
if(Serv.Service_Name__c == 'Essential Property Education')
{
        EmailHandler.sendEPEEmail(lcontactEmails);
    } 
    }   
}
}


OnpursuitOnpursuit
Hi,

The second one seems good. There is no for loop inside for loop and you are making list to send emails. That looks a cleaner approach.

Thanks
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Thank you for the reply, Currently it sends it out to a mass email messaging through an email handler. If I wanted to try and send multiple single messages instead could I use the same body of the trigger or would I have to change it? When I tried to send it out to a single messaging section of the EmailHandler class(basically using the template of the massemail ones) except it would not work the same as it does for mass mailer. I think usually it errors when I try and save it and the error is from setting the contact part. Thank you for your time