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
Kunal Purohit 4Kunal Purohit 4 

How to create Email logs into custom Object?

Hello Guys,
I am having Send Email Button in Case Object. Now whenever Email is being sent, its ToAddress, BCC,CC and TemplateName should be copied into custom Object Email_History__c, having similar fields. How to perform this task? Please sugggest.
 
SubratSubrat (Salesforce Developers) 
Hello Kunal ,

To perform this task, you can use Apex triggers in Salesforce to capture the necessary information and create a record in the custom object, Email_History__c. Here's an example of how you can achieve this:
trigger SendEmailTrigger on Case (before insert, before update) {
  List<Email_History_c> emailHistoryList = new List<Email_History_c>();
  
  for (Case caseObj : Trigger.new) {
    if (caseObj.Send_Email_c) {  // Assuming Send_Email_c is a checkbox field on Case representing the Send Email button
      Email_History_c emailHistory = new Email_History_c();
      emailHistory.ToAddress_c = caseObj.ToAddressc;  // Assuming ToAddress_c is a field on Case
      emailHistory.BCC_c = caseObj.BCCc;  // Assuming BCC_c is a field on Case
      emailHistory.CC_c = caseObj.CCc;  // Assuming CC_c is a field on Case
      emailHistory.TemplateName_c = caseObj.TemplateNamec;  // Assuming TemplateName_c is a field on Case
      
      emailHistoryList.add(emailHistory);
    }
  }
  
  if (!emailHistoryList.isEmpty()) {
    insert emailHistoryList;
  }
}
With this trigger in place, whenever a Case record is inserted or updated and the Send Email button is clicked (assuming you have a checkbox field to represent this), the trigger will create a record in the Email_History__c object with the relevant information.

If this helps , please mark this as Best Answer.
Thank you.