0

I have a simple batch job that is used to send a notification email. I'm testing in a partial sandbox, but the email is not coming thru. Here is what I've checked:

  • I verified that deliverability is set to "All Email"
  • I checked the limits in workbench which shows I have 4993 out of 5000 single email limits remaining
  • I've debugged the code and the sendEmailResults shows the email was sent successfully
  • I've also tested deliverability and verified that the emails from Salesforcecomethru to my email address and I received the emails

Here is the code:

global Database.querylocator start(Database.BatchableContext BC){
    return Database.getQueryLocator('select Id, StageName, Owner.Email from Opportunity where Role_Record_Count__c = 0 AND CloseDate < NEXT_90_DAYS AND Project_Start_Date__c < NEXT_90_DAYS AND (StageName = \'B - Value Justification\' OR StageName = \'A - Pending Sales\')');
} 

global void execute(Database.BatchableContext BC, List<sObject> scope) {
    Messaging.SingleEmailMessage emailMsg = new Messaging.SingleEmailMessage();
    List<String> toAddresses = new List<String>();
    for(Opportunity o : (List<Opportunity>)scope) { 
        toAddresses.add(o.Owner.Email);
    }
    system.debug('******************************************** toAddresses ' + toAddresses);
    emailMsg.setToAddresses(toAddresses);
    emailMsg.setSubject('Test Subject!!');
    emailMsg.setPlainTextBody('Hello!!!!!!!!!!This is a test email to test single email message program');
    emailMsg.setHtmlBody('Hello!!!!!!!!!!This is a test email to test single email message program');
    Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {emailMsg};
    Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
    if (results[0].success) {
        System.debug('The email was sent successfully.');
    } else {
        System.debug('The email failed to send: ' + results[0].errors[0].message);
    }
} 

global void finish(Database.BatchableContext BC) {

} 

Anyone have any idea what might be preventing the email from being sent in a partial sandbox? Is there anything wrong with the code?

  • Have you unmunged the relevant Users' email addresses? All except the user who refreshed the sandbox will have the @ replaced with = and @example.com appended. – Thomas Taylor Aug 19 '19 at 0:15
  • yes, I made sure the emails were correct in the sandbox. – Dman100 Aug 19 '19 at 0:50
  • Not to belabor the point, but they won't be actually changed until the user clicks thru on the notification. You're so not setting a From, so I think they'd come from the user that started the Batch, but maybe the From is weird & it's getting filtered, either into spam or at the server level. Generate an email log and check delivery there. And maybe create a new user and make it owner of some opps. – Thomas Taylor Aug 19 '19 at 1:00
  • I made sure that I was the user that should be receiving the emails by changing the email of the owner of the opportunity and I did click on the notification to update the email and verified it had been changed. I also generated an email log and it shows an event log of D. So, it indicates that the email was delivered. It must be something on the email server side blocking the emails. – Dman100 Aug 19 '19 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.