3

I am writing a trigger to send email to all contacts of an account when it is edited.

trigger SendEmailToContacts on Account (after update) 
{   
 try{  
   String addresses;
    for(Account acc : Trigger.new)
        {system.debug('e2='+acc.contacts);
        system.debug('e2='+acc.contacts.size());
            for(Contact con : acc.contacts)
            {system.debug('e1='+addresses);
                if(con.email != null)
                {
                    addresses = addresses  + ':'  + con.email;
                    system.debug('e='+addresses);
               }
            }   

            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            List<String> emailAddresses = new List<String>();
            emailaddresses = addresses.split(':',0);
            message.ToAddresses = (emailaddresses);
            message.subject = 'congrats';
            message.plainTextBody = 'you a test account.';
            List<Messaging.SendEmailResult> results = Messaging.sendEmail(new List<Messaging.SingleEmailMessage> {message});                   

          } 
    }catch(Exception ec)
           {    
              system.debug('error is' + ec);
  }
                  }

The control is not going in second for loop

for(Contact con : acc.contacts)

4

Triggers will not have information about parent or child records .You will have to explicitly query them back .

trigger SendEmailToContacts on Account (after update) {
 map<id,Account> accountMap = new map<id,Account>(Select Id,(Select Id,Name from Contacts) FROM Account);
try{  
  String addresses;
  for(Account acc : accountMap.values())
    {system.debug('e2='+acc.contacts);
    system.debug('e2='+acc.contacts.size());
        for(Contact con : acc.contacts)
        {system.debug('e1='+addresses);
            if(con.email != null)
            {
                addresses = addresses  + ':'  + con.email;
                system.debug('e='+addresses);
           }
        }   

        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        List<String> emailAddresses = new List<String>();
        emailaddresses = addresses.split(':',0);
        message.ToAddresses = (emailaddresses);
        message.subject = 'congrats';
        message.plainTextBody = 'you a test account.';
        List<Messaging.SendEmailResult> results = Messaging.sendEmail(new List<Messaging.SingleEmailMessage> {message});                   

      } 
}catch(Exception ec)
       {    
          system.debug('error is' + ec);
        }
 }

Although this is not part of this question would like to point few things that will not work for you

1.There is a chance that you will easily hit the email limits with this code .

Please try to move Messaging.sendEmail outside FOR loop

2.Always use setTargetObjectId() method of SingleEmailMessage as it avoids limits

3.Follow a trigger handler pattern and do not code anything inside trigger .Its hard to maintain and debug .

4.Lastly you do not need two FOR loops here .You can just loop over the contact records and use relationship reference back to account .

| improve this answer | |
  • I think you mean setTargetObjectId() and how does this avoid limits (like the 1000 external email addresses per day limit? Am I out of date here? See Apex Email Limits – cropredy Aug 21 '16 at 18:41
  • Please read those docs that you posted now and check for setTargetObjectId() documentation .The email that user is sending is Contact Email Id and setTargetObjectId() will avoid this :) – Mohith Shrivastava Aug 21 '16 at 18:46
  • Mohith -- I thought about that and should run an experiment - when I read the doc link - my understanding was that only if setTargetObjectId() was a User would the limits not apply. One can send email to Contacts through the 'application' without limits applying - which I always took to mean sending via the 'send email button' or email alerts via workflow or process builder – cropredy Aug 22 '16 at 1:10
  • @cropredy Experimented with the same and emails run without any limits :) – Mohith Shrivastava Aug 22 '16 at 1:43
  • cool - I learned something :-) – cropredy Aug 22 '16 at 4:15

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.