4

I populate a list of emails emailsToBeSent and send them all at once. However These emails are related to different sObjects. And to me it is important to know which sObject the returned results are related to in the case of an error.

Here's the code I have right now:

Messaging.sendEmail(emailsToBeSent);

For that I get a list of results with a isSuccess() value. How can I know which result is related to what sObject?

My only hope is - perhaps if the returned list of results keeps the order of the emails from the emailsToBeSent list. Can anybody confirm or reject this idea?

7

To answer the specific question, the sendEmail method returns a list of Messaging.SendEmailResult objects. This list will be in the same order as the corresponding list of emails provided to sendEmail(..).

As Mohith pointed out, you can only discover whether the email message was successfully queued for delivery, and, if not, why. You can't discover if the email was actually sent, let alone received.

So, to correlate to the Sobjects in question, you can do either:

  • Maintain a map of EmailMessage index position (within the list) to SObjectId or ..
  • Iterate through the Messaging.SendEmailResult list as shown below

    for (Messaging.SendEmailResult ser: Messaging.sendEmails(emailList))
       if (!ser.isSuccess())
          // do something with ser.getErrors().getTargetObjectId()
    
| improve this answer | |
  • Awesome, thank you so much! That's exactly what I was planning on! – Arthlete Mar 18 '16 at 19:45
  • 2
    a final tip -- emails are never sent in testmethods so don't expect to see any of them in your inbox – cropredy Mar 18 '16 at 20:04
3

First of all isSuccess=true does not mean the Email was sent .Carefully look at the documentation

Even if success = true, it does not mean the intended recipients received the email, as it could have bounced or been blocked by a spam blocker. Also, even if the email is successfully accepted for delivery by the message transfer agent, there can still be errors in the error array related to individual addresses within the email.

So the best way to track this is via reports in SFDC

https://www.shellblack.com/administration/email-deliverabilty-and-html-status-reports/

Check this article shows how as an admin you can enable Email Tracking and track the email in the related list .

| improve this answer | |
  • Thanks for the response @MohithShrivastava. Always knowledgeable. However can you tell me how can I relate the results to a particular sObject? That's what is more important to my project at the moment. I need to be able to know which results are related to what sObject. – Arthlete Mar 18 '16 at 18:15
  • 1
    Use setTargetId() method and use setSaveAsActiviy() to create activity . – Mohith Shrivastava Mar 18 '16 at 18:49
  • 1
    The results will show up in results for you out of box – Mohith Shrivastava Mar 18 '16 at 18:49

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.