Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

trigger sendEmailtoEMDR on Whatever_object__c (after insert) {

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

  for (Whatever_object__c newTT : {

    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>();


      List<String> ccTo = new List<String>();

      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';


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. – Chris Duncombe May 9 '14 at 18:28
up vote 5 down vote accepted

In your Trigger, the value of newTT will not have access to assigned_user__r fields. The 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.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 : {
   Whatever_Obj__c newTT = woIdToWhateverMap.get(;
   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, firstname etc. – user1122397 May 10 '14 at 1:12

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

Your Answer


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.