1

I am trying to send an email using an Assignment Template from my trigger when the status is changed by the appropriate team.

I looked around and found 17047778: sending-email-with-template-using-trigger which is where I got some of the code from.

Here is the code in my trigger - it runs on Trigger.IsUpdate && trigger.isbefore and the loop for (Request_for_System_Change__c rq : Trigger.new) { :

// send conversion email 

// 1. create a new single message                  
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

// 2. set the email addresses
List<String> sendTo = new List<String>();
string emailAddy=string.valueOf([SELECT Email from User where Id =: rq.Manager__c].Email);
sendTo.add(emailAddy); 

emailAddy=string.valueOf([SELECT Email from User where Id =: rq.Originating_Owner__c].Email);
sendTo.add(emailAddy);

mail.setToAddresses(sendTo);
mail.setTargetObjectId(rq.Manager__c);  // sendTo doesn't work either

// 3. set the template 
EmailTemplate et=[Select id from EmailTemplate where DeveloperName ='Data_Request_Converted'];                          
mail.setTemplateId(et.id);

// 4. send the email
Messaging.SendEmailResult [] r = 
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail}); 

Here is the error returned by SF:

Apex trigger System_Change_Request_Events caused an unexpected exception, contact your administrator: System_Change_Request_Events: execution of BeforeUpdate caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing targetObjectId with template: []: Trigger.System_Change_Request_Events: line 250, column 1

it seems to be saying something about the template... I got the template info from the query:

Select Id, Name from EmailTemplate where DeveloperName='Data_Request_Converted'

so I know the assignment template exists..

What am I doing wrong?

  • This error means rq.Manager__c is null. – Adrian Larson Feb 16 '16 at 18:48
  • @AdrianLarson: Thanks for your help Adrian, but rq.Manager__c isn't null, I can get get it in the debug logs – Our Man in Bananas Feb 16 '16 at 18:52
3

Cause

From the Messaging.SingleEmailMessage documentation, you cannot use setToAddresses in conjunction with setTemplateId:

setToAddresses(toAddresses)

Optional. A list of email addresses to which you are sending the email. The maximum number of email addresses allowed is 100. This argument is allowed only when a template is not used.

Limits

Note that you are blowing through email limits faster than you need to be because using a targetObjectId whose type is User is exempt from the daily send limit:

If you use SingleEmailMessage to email your organization’s internal users, specifying the user’s ID in setTargetObjectId means the email doesn’t count toward the daily limit. However, specifying internal users’ email addresses in setToAddresses means the email does count toward the limit.

Usage

If you want to send emails to multiple users, create multiple SingleEmailMessages for each targetObjectId or one MassEmailMessage.

MassEmailMessage massEmail = new MassEmailMessage();
massEmail.setTemplateId(myTemplateId);
massEmail.setTargetObjectIds(myUsers);
//send
  • so I should use setTargetObjectId? But when I tried mail.setTargetObjectId(sendTo); I get a compile error: Method does not exist or incorrect signature: [Messaging.SingleEmailMessage].setTargetObjectId(List&lt;String&gt;) even though sendTo is a List defined as List<String> sendTo = new List<String>(); – Our Man in Bananas Feb 16 '16 at 19:00
  • Thanks for your help, I got the email to be sent, but I guess I need to send it several times, once for each recipient... – Our Man in Bananas Feb 16 '16 at 19:02
  • 1
    @OurManInBananas Not so if you use MassEmailMessage, but yes if you want to do SingleEmailMessage. See my updates. – Adrian Larson Feb 16 '16 at 19:04
  • 1
    Unfortunately you can only call setWhatId if you are emailing Contact records. From my first link: "If you specify a contact for the targetObjectId field, you can specify an optional whatId as well. This helps to further ensure that merge fields in the template contain the correct data." – Adrian Larson Feb 16 '16 at 19:07
  • 1
    No problem. Also awesome username. Anyway maybe this post will help a little. I don't think you can use MassEmailMessage for merging, but I've never tried it. – Adrian Larson Feb 16 '16 at 19:12

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.