function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AngrySlothAngrySloth 

Apex Email not creating record in Activity History in Production only

HI All,

I am very confused and stuck at the moment. I have the following Apex code being fired in a trigger:
 
private static void sendMercuryMotorOrderEmail( Set<Id> dealerOrderIds )
  {
    String[] ccAddrs = new List<String>{
      'dealercare@legendboats.com'
    };
    //email template we want to use
    String templateId = Legend_Settings__c.getOrgDefaults().Mercury_Order_Email_Template_Id__c;
    //Static contact we want to email
    Id contactId = Legend_Settings__c.getOrgDefaults().Mercury_Contact_Id__c;
    List<Messaging.SingleEmailMessage> msgList= new List<Messaging.SingleEmailMessage>();
    for( Id doId : dealerOrderIds )
    {
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      mail.setTargetObjectId(contactId);
      mail.setWhatId( doId );
      mail.setTemplateId( templateId );
      mail.setCcAddresses( ccAddrs );
      mail.setSaveAsActivity(true);
      msgList.add(mail);
    }
    Messaging.sendEmail(msgList);
  }

In my sandbox environment ( which was refreshed from production very recently and none of this code has changed ) the email is sent as expected and the Email shows up in the Activity History on the record details page in Lightning.  In production the email is sent correctly but the nothing ever shows in the Activity History.  Furthor more ( probably the reason for no Activity History ) in Production no record in the Email Message table is being created, but in Sandbox there is.

I have check all permissions and settings in Production I can think of ( yes Track Activities is enabled on the object ).  Is there anything I am possibly missing?  My users really want this to show on the activity timeline and I am just completely confused.

Thanks a alot for any assistance.
Alain CabonAlain Cabon
First check:

Considerations for Setting Up Enhanced Email
  • With Enhanced Email, emails sent from Salesforce are saved as both EmailMessage records and Task records. However, only the EmailMessage record is shown on the email detail page in the UI.
  • Email is logged as an EmailMessage record, not an Activity record, and how you modify the content has changed. Email fields, including RelatedToId, can only be changed when the email is in draft status. 
https://help.salesforce.com/articleView?id=emailadmin_enhanced_email_considerations.htm&type=0

Set Up Enhanced Email
  1. From Setup, enter Enhanced Email in the Quick Find box, then select Enhanced Email.
  2. Click Enable.
https://help.salesforce.com/articleView?id=enable_enhanced_email.htm&type=5
AngrySlothAngrySloth
Thanks @Alain Cabon

Unfortunately Enhanced Email is enabled in our production org as well as the Sandbox.
Alain CabonAlain Cabon
I just tried Salesforce Org Compare (Heroku) but I never got the result ...

And not sure, all the configuration will be compared (like Setting Up Enhanced Email).

Salesforce Org Compare
This tool uses the Salesforce Tooling or Metadata API (user selection) to compare metadata between two Orgs. This is useful when planning deployments or building deployment packages, as well as seeing what configuration exists in Production to Sandbox or between Sandbox environments.
None of your organisation information or data is captured or kept from running this tool.

https://sforgcompare.herokuapp.com/

Salesforce Toolkit
Welcome to the Salesforce Toolkit! The tools and applications below provide assistance for common consulting processes and tasks - from diagnosing your org, comparing environments to activating and deactivating Salesforce components.

http://sftoolkit.co/

From New-Zeland.
AngrySlothAngrySloth
THanks again @Alain.  I ran the Salesforce Toolkit compare Orgs function and surprisingly there are no substantial differences between the sandbox and production orgs, other then a few changes I have made in the sandbox since the refresh.  I am really out of ideas
Alain CabonAlain Cabon
Hi @AngrySloth

I tried your code as anonymous code and there is no problem of timeline for the contact but there is not a lot of thing in the log.

You can compare the results for the two org with an anonymous snippet but the email queue log is simple (subject, address, htmlbody).

setSaveAsActivity(true); is the default and you have use an html template. Your mails are equals (both are HTML templates).

EMAIL_QUEUE [17]|subject: Very important, bccSender: false, saveAsActivity: true, useSignature: true, ccAddresses: [xxxx.xxxx@xxxx.xxx], targetObjectId: 00358000013Nmjo, templateId: 00X58000001Ast0, plainTextBody: Hello test test56, htmlBody: <style>p{margin-top:0px; margin-bottom:0px;}</style><body class="setupTab"  style=" background-color:#CCCCCC; bEditID:b1st1; bLabel:body;"><center ><table cellpadding="0" width="500" cellspacing="0" id="topTable" height="450" ><tr valign="top" ><td  style=" background-color:#FFFFFF; bEditID:r1st1; bLabel:header; vertical-align:top; height:100; text-align:left;"></td></tr><tr valign="top" ><td  style=" background-color:#AAAAFF; bEditID:r2st1; bLabel:accent1; height:5;"></td></tr><tr valign="top" ><td styleInsert="1" height="300"  style=" background-color:#FFFFFF; color:#000000; bEditID:r3st1; bLabel:main; font-size:12pt; font-family:arial;"><table border="0" cellpadding="5" width="550" cellspacing="5" height="400" ><tr valign="top" height="400" ><td tEditID="c1r1" style=" background-color:#FFFFFF; color:#000000; bEditID:r3st1; bLabel:main; font-size:12pt; font-family:arial;" aEditID="c1r1" locked="0" >Hello&nbsp;test test56</td></tr></table></td></tr><tr valign="top" ><td  style=" background-color:#AAAAFF; bEditID:r4st1; bLabel:accent2; height:5;"></td></tr><tr valign="top" ><td  style=" background-color:#FFFFFF; bEditID:r5st1; bLabel:footer; vertical-align:top; height:100; text-align:left;"></td></tr><tr valign="top" ><td  style=" background-color:#AAAAFF; bEditID:r6st1; bLabel:accent3; height:5;"></td></tr></table></center>, 

User-added image

Anonymous code:
    String[] ccAddrs = new List<String>{
      'xxxx.xxxx@xxx.xxx'
    };
    //email template we want to use
    String templateId = '00X58000001Ast0';
    //Static contact we want to email
    Id contactId = '00358000013NmjoAAC';
    List<Messaging.SingleEmailMessage> msgList= new List<Messaging.SingleEmailMessage>();
  
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setTargetObjectId(contactId);
 //     mail.setWhatId( doId );
    mail.setTemplateId( templateId );
    mail.setCcAddresses( ccAddrs );
    mail.setSaveAsActivity(true);
    msgList.add(mail);
    Messaging.sendEmail(msgList);

As long as the emails are both in HTML, you should have a filled timeline.

I don't know. 

You could try other forums for admin ( community ). 
 
AngrySlothAngrySloth
Thanks for your assistance Alain, I goning to have to log a case with Salesforce
Swapnil KhambayatSwapnil Khambayat
hey AngrySloth,

Have you got root cause for this issue. I am facing similar issue. please reply. thaks in advance :)

Regards,
Swapnil