How to show EmailMessage Last Opened Time on activity and associate activity to account, Contact and Custom Object in Salesforce using Apex

August 24, 2023


 

 

In this blog post, we will learn about show emailMessage open time and link email message as activity on account, contact and custom object using apex code.

After completing this unit, you’ll be able to:

 

How To Track EmailMessage in Salesforce

To enable the track of emailMessage in salesforce you need to follow below steps:

  1. Go to quick find in salesforce org and search activity settings
  2. Click on activity settings
  3. below screen will appear
  4.  

     

  5. Check the Enable Email Tracking box
  6. Click on save button

After enable above email tracking setting email will show last opened time when you send email through salesforce org.

 

 

How To Link EmailMessage as Activity on Account, Contact and Custom Object Using Apex

 

To link emailMessage with account, contact and custom object we will use visualforce email template.

First of all we will create email template with the type of visualforce

Creating Visualforce Email Template in Salesforce 

  1. Go to quick find in salesforce org and search Email Template
  2. Select Classic Email Templates
  3.  

     

  4. Click on New Template button
  5.  

     

  6. Select Visualforce and click on next button like this
  7.  

     

  8. Fill the form like this
  9.  

     

     

          5.1 Folder = Choose folder you want to store email template

          5.2 Available For Use = Select the Checkbox

          5.3 Email Template Name = DemoTemplate 

          5.4 Template Unique Name = Automatically populate when Template Name will fill

          5.5 Email Subject = Test Subject

          5.6 Recipient Type = Select Contact

          5.7 Related To Type = Select Account

       6. Click on Save button

       7. Click on Edit Template button

 

 

       8. If you want to show email body from Template then you need to remove <messaging:PlainTextEmailBody> tag and add <messaging:HtmlEmailBody > and write your body      message because plain text email body tag does not track the email only html body tag allows tracking of email in salesforce.

 

After creating email template now we will write the apex code you can copy and paste below code into your apex class

 

Apex code

 

  public static void sendEmailMessage()

  { 
         Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();    
         string[] toAddress = new string[] {'test@test.com'};
         message.setToAddresses(toAddress); 
         EmailTemplate template =[Select Id from EmailTemplate where DeveloperName='DemoTemplate'];
         message.setTemplateId(template.Id);   
         message.setTargetObjectId(ContactId);   
         message.setWhatId(CustomObjectId);
         message.setSaveAsActivity(true);
         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { message});
  }
 
After run above code in apex the email message will show on activity section on account, contact and custom object as you mentioned in apex code.
 
The Activity Email Message will look like this:
 

 

 

 

I hope this blog helped you!