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
SFDC 2017SFDC 2017 

Capture the details of the Email sent through Apex into a Custom Object

Hi All,
I want to track what are the Email Notifications has been sent from Apex code  .So i want to store whenever an email is send from Custom Object i need to store all the details of that Email like subject ,to Everything into a custom Object..Below is the code:
  Set<String> emailSet = new Set<String>();
                        emailSet.addAll(emailAddress);
                        emailAddress = new List<String>();
                        emailAddress.addAll(emailSet);
                        emailAddressMapWithAccountId.put(acc.Id, emailAddress);
                        
                        Set<String> emailSetBcc = new Set<String>();
                        emailSetBcc.addAll(emailAddressBcc);
                        emailAddressBcc = new List<String>();
                        emailAddressBcc.addAll(emailSetBcc);
                        emailAddressMapWithAccountIdBcc.put(acc.Id, emailAddressBcc);
                        
                        mail = new Messaging.SingleEmailMessage();
                        
                        mail.setToAddresses(emailAddress);
                        mail.setBccAddresses(emailAddressBcc);
                        mail.setTemplateId(templates.get(0).Id);
                        if(contactIDSet.size() > 0){
                            mail.setTargetObjectId(contactIDSet.get(0));
                            mail.setWhatId(CustomobjectID);
                        }else{
                            if(con == null || con.Id == null) {
                                con=[SELECT Id FROM Contact Limit 1]; 
                            }
                            mail.setTargetObjectId(con.id);
                            mail.setTreatTargetObjectAsRecipient(false);  
                            mail.setWhatId(CustomobjectID);
                        }
                        
                        mailArray.add(mail);
After this i m calling one common method to insert the Record of Email into Custom Object called
TrackEmailDetails(What are the Parameters to be passed);
 but i am not sure how to proceed with getting the Template body and To Addresses everything.I want something like below as 
EmailMessage emailMsg = new EmailMessage(); // Created a EmailMessage and copy all details from above.
emailMsg.ToAddress=(emailList[0].getToAddresses())[0];
emailMsg.Subject=emailList[0].getSubject();
emailMsg.HtmlBody=emailList[0].getHtmlBody();
 insert emailMsg;
This is not bulkified .Can anyone please suggest idea how to pass the parameter so i can use this common method anytimes wherever is required

Thanks in Advance

 
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Archu,
May I suggest you please refer the below link for reference. Thanks
Rahul Kumar