Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I'm trying to send an email to a user when a new custom object is created.

This currently doesn't work in the sandbox. Are there any special settings that need to be enabled or is there a bug is the code? I checked Email Administration and 'All Emails' is enabled.

Thanks for any help

P.S. I know I can also do a workflow rule, but the next step is to also send an email when deleted so need to have some code working either way.

/* Send Email to MDR when they are added to Whatever object. 
*
* @author me - code taken from sfdc99.com
*/

trigger sendEmailtoEMDR on Whatever_object__c (after insert) {

  List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();

  for (Whatever_object__c newTT : Trigger.new) {

    if (newTT.Assigned_User__r.Email != null && newTT.Assigned_User__r.FirstName != null && 
        newTT.CreatedBy.Email != null && newTT.CreatedBy.FirstName != null) {

      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

      List<String> sendTo = new List<String>();
      sendTo.add(newTT.Assigned_User__r.Email);
      mail.setToAddresses(sendTo);

      mail.setReplyTo(newTT.CreatedBy.Email);
      mail.setSenderDisplayName(newTT.CreatedBy.FirstName);

      List<String> ccTo = new List<String>();
      ccTo.add('an_email@gmail.com');
      mail.setCcAddresses(ccTo);

      mail.setSubject('A new Whatever Object was assigned to you: ' + newTT.Account__r.Name);
      String body ='<html><body>'+ newTT.Assigned_User__r.FirstName + ', <br><br>' + 
      newTT.CreatedBy.FirstName + 'assigned' + newTT.Account__R.Name + 'as a new Whatever object';
      mail.setHtmlBody(body);

      mails.add(mail);
    }
  }

  Messaging.sendEmail(mails);
}
share|improve this question
    
Have you made sure the email address of the user is correct? In the Sandbox the email address adds a '.sandboxname' to the end of the email address. –  sfdc_ninja May 9 at 18:28
add comment

2 Answers 2

up vote 5 down vote accepted

In your Trigger, the value of newTT will not have access to assigned_user__r fields. The trigger.new List only knows about the fields in Whatever_object__c, not any relationship fields, these have to be queried for as in:

Map<ID,Whatever_obj__c> woIdToWhateverMap = new Map<ID,Whatever_obj__c> (
      [select id, assigned_user__r.email, assigned_user__r.firstName, ...
         where id IN :Trigger.newMap.keySet()]);

Then use the map to do your test for email eligibility as in

for (Whatever_obj__c wo : Trigger.new) {
   Whatever_Obj__c newTT = woIdToWhateverMap.get(wo.id);
   if (newTT.Assigned_User__r.Email != null && newTT.Assigned_User__r.FirstName != null && 
       newTT.CreatedBy.Email != null && newTT.CreatedBy.FirstName != null) {
   ... build the email
  }
}
share|improve this answer
    
Thanks! That did the trick. Interesting that I could save that query in the Whatever_obj__c custom object, even though there aren't specific fields for assigned_user__r.email, firstname etc. –  user1122397 May 10 at 1:12
add comment

Check your email settings to make sure that all types of email are enabled. Sandboxes have non-system emails, like workflow and apex email, turned off by default when they are created/refreshed.

To check your email settings, go to Setup > Email Administration > Deliverability and make sure the access level is set to All Email.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.