[SalesForce] how to store email sent from apex in email related list

I have implemented a functionlity in apex which on click of button sends email to contacts of specific case id.

I want is that the email being sent should get stored in email related list of that case id.

Could you please let me know how i can achieve that?

My apex code for sending email is below :

//get the last inserted attachment
         Attachment attach = [SELECT Body,ContentType,Name FROM Attachment where parentId = :caseid  ORDER BY CreatedDate DESC LIMIT 1];

         Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
          If(attach != null){

         //list for sending multiple message at once
         list<Messaging.SingleEmailMessage> lstemail = new list<Messaging.SingleEmailMessage>();

         //draft email to send to secondary contact
        Messaging.SingleEmailMessage mailHandler = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {objContact.RMA_Secondary_Contact_Email__c};
        mailHandler.setSubject('RMA Form');
        mailHandler.setPlaintextbody('Hello ' + objContact.RMA_Secondary_Contact_First_Name__c+'\n\n Please Find the pdf attached\n\nRegards\nXyz');
        System.debug('messagetext' +mailhandler);
        mailHandler.setFileAttachments(new Messaging.EmailFileAttachment[] { efa });

        //add email to list

        //draft email to send to primary contact
        Messaging.SingleEmailMessage mailHandler1 = new Messaging.SingleEmailMessage();
        String[] toAddresses1 = new String[] {objContact.Contact.Email};
        mailHandler1.setSubject('RMA Form');
        mailHandler1.setPlaintextbody('Hello ' + objContact.Contact.Name+'\n\n Please Find the pdf attached\n\nRegards\nXyz');
        System.debug('messagetext' +mailhandler1);
        mailHandler1.setFileAttachments(new Messaging.EmailFileAttachment[] { efa });

        //add email to list

        //send all the email in list and get the result
        Messaging.sendEmailResult[] r= Messaging.sendEmail(lstemail);

Best Answer

Add the following line to your code. This email will be shown in activity history.

mailHandler.setSaveAsActivity(true); //add mail to the activity history

Update 6/25/2015

2nd Update 6/26/2016

Here is a working solution. There is no tag available to directly assign email to a case. What you can do, create a EmailMessage copy all details from your SingleEmailMessage to this and attach to the case. And you can see it in the email related list.

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSubject('Email using Apex');
String[] toAddresses = new String[] {'abc@def.com'};
email.setHtmlBody('<html><body>Hi <b>Saroj</b></body></html>');
Messaging.SingleEmailMessage[] emailList = new Messaging.SingleEmailMessage[] {email};
Case case_temp = [SELECT Id FROM Case where Id = 'XXXXXXXX' LIMIT 1]; // I have tried this code with hard coding a case Id
EmailMessage emailMsg = new EmailMessage(); // Created a EmailMessage and copy all details from above.
emailMsg.ParentId = case_temp.Id; //Attach with the case
emailMsg.MessageDate = system.now();
emailMsg.Status = '0';
insert emailMsg;
    Attachment[] attachments=new Attachment[]{};
    Attachment attachment=null;
    for(Integer i=0;i<emailList[0].attachments.size();i++){
        attachment= new Attachment();
        attachment.Name = emailList[0].attachmentNames[i];
        attachment.Body = emailList[0].attachments[i];
        attachment.ParentId = emailMsg.Id; 
    insert attachments;
Related Topic