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 

Need Activity history for custom object if an email is send from Apex Email Class Setsaveasactivity(true) is not working

Hi All,
I want to track what are the Email Notifications has been sent from my custom Object .So i want to store whenever an email is send from Custom Object i need to store in Activity History .I used SetSaveAsActivity(true) is not working.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.setSaveAsActivity(true);
                            mail.setTreatTargetObjectAsRecipient(false);  
                            mail.setWhatId(CustomobjectID);
                        }
                        
                        mailArray.add(mail);

I am not sure why SetSaveAsActivity is not working.I want to track all email under my custom object

Thanks in Advance
                    }
                }
            }
NagendraNagendra (Salesforce Developers) 
Hi Archu,

May I suggest you please check with below link from the stack exchange community exactly with a similar issue which will guide you in the right direction. Please let us know if this helps.

Kindly mark this post as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Thanks,
Nagendra