How to add Activity History while sending emails in Salesforce

H

Hi Friends, many of you know the Salesforce SingleEmailMessage method,In fact you have used it in your apex code. But when we send mail through this method by default it doesn’t add mail into related list of recipient’s activity history.
If you are stuck with this type of issue, then below I’m providing the solution for this.

setSaveAsActivity(true);

Add this method into your code which will add sent mail to the activity History related List.

Follow the example given for better understanding:
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[]{‘Receipient’s Email’};
String[] ccAddresses = new String[] {‘Additional Receipient’};
mail.setTargetObjectId(receipient.id); // Put setTargetobjectid value as per your requirement.
mail.setCcAddresses(ccAddresses);
mail.setSenderDisplayName(‘SalesforceLogin Test’);
mail.setSubject(‘Subject Test’);
mail.setSaveAsActivity(true); //add mail to the activity history
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setHtmlBody(‘Hi! Salesforce user’);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

Hope this post Helps You!

 

About the author

trupti.dhoka
By trupti.dhoka

Category