1

I'm wondering if anyone can help. I have a trigger that uses a helper class to send an email using the sendMail method. This code is written in one of my Sandboxes and when I test this functionality through my test classes and check the debug logs it looks like the send is successful.

I have also tested this by turning on the debug logs when doing a manual test through the Salesforce UI, again the debug logs show a successful send to my email address, however, I don't receive that email in my inbox.

I have tried to resolve this myself by looking up solutions from other people who have reported similar problems but so far I have been unsuccessful.

Deliverability is set to 'All Emails' and I have changed my email address in Sandbox to ensure that emails get sent to my actual email address. I have been able to receive other notification emails from my Sandbox but any time I try to run my apex code by inserting a duplicate lead with the same email address (this is one scenario), the addError appears on the record page and the logs indicate that the email was sent, but I don't receive the email and requesting email logs for the time it was sent don't show the email as being sent out of Salesforce.

Here's my code snippets:

if(existingLeadsByEmail.containsKey(l.Email){
    matchOnEmail = true;
    buildEmailMessage(existingLeadsByEmail.get(l.Email), l);    
    l.addError('A lead with this email address already exists.'); 
}

The buildEmailMessage creates the email and adds it to a list of emails to be sent after all incoming leads have been processed. Then to send the email I use the following:

if(allMails.size() > 0){
    List<Messaging.SendEmailResult> results = Messaging.sendEmail(allMails); 
    if(!results.get(0).isSuccess()){        
         System.debug('Send Error: ' + results.get(0).getErrors()[0].getMessage());     
    } else {        
         System.debug('Email Send Successful!');    
    } 
}

Any help on this would be greatly appreciated.

Many thanks guys!

  • 1
    How are you executing this code? Emails won't be sent if you're executing them in a test context – Brian Mansfield Jul 14 '15 at 17:03
  • emails will be rolled back if the transaction fails (i.e. using addError) – cropredy Jul 14 '15 at 17:17
  • Thanks for the replies. Yes I am aware that no emails get sent when running my test classes - I was performing a lead insert through the Salesforce UI and when I received the error notifying that a duplicate exists I assumed the code would send the email. I wasn't aware that the email would not be sent because the transaction failed. I need to be able to send a notification email to the existing lead owner but also prevent the new duplicate lead from being created - hence why I am using addError. So is there any solution that will allow me to send the email after the transaction fails? – Brian Casey Jul 14 '15 at 21:13
  • so your general problem is that you don't want the Lead to be inserted (it is a duplicate) but you want to notify someone that a duplicate insertion was attempted? Are you using a VF page by any chance or standard Lead pageLayout? and .. SFDC Duplicate management usable by you? help.salesforce.com/apex/… – cropredy Jul 14 '15 at 21:51
  • thanks for your reply crop1645, I am using a Standard Lead page layout. Do you think creating a VF page would be a solution? If so, how. AS regards the Duplicate management feature - I will investigate to see if it meets our requirements. If you can provide any advice on the VF page option I would be very interested in this. Many thanks for your help. – Brian Casey Jul 15 '15 at 9:30
1

You may want to check the "Access to Send Email" setting in your Setup -> Administration Setup -> Email Administration -> Delivery. Set it to "All email". I believe SendEmail does not work if this is set to "System email only".

I found in my sandbox, even though it was refreshed anew that this setting wasn't the same as my live environment, and caused the same issue as you are experiencing.

| improve this answer | |
  • Thanks for your response but as mentioned in my question I already have this setting enabled. – Brian Casey Jul 15 '15 at 11:30
0

It appears that this is not possible in Salesforce as per the idea in the Success Community: https://success.salesforce.com/ideaView?id=08730000000l0UcAAI&sort=2

I have voted and commented on this but it's very disappointing that we now have to devote more time than we anticipated to investigate alternatives to meet our requirements because Salesforce does not have this functionality in place.

Thanks for your replies and comments guys in trying to help me with this.

| improve this answer | |

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.