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
StaciStaci 

create email message activity history with inbound email handler

I have a custom object with a custom Send An Email button that will default the Related To in the subject line of the outbound email message.  It sends fine.  What I want, is if anyone replies to that email I want another entry made in the Activity History of that inbound email message.  I can see in the debug logs that the emails are coming back in, but I can't find them anywhere (floating around in SF space!)  How do I get them associated with the custom object when they come back into SF?
Here's my email handler
global class serviceInquiryHandler implements Messaging.InboundEmailHandler {
         
          
      global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
          Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
          
      if(email.subject.contains('Service Inquiry'))
      {
          
            
            try
            {
                 //string ccAddy = envelope.ccAddress;
                 system.debug('I get here' + email.subject + '--------------------------------------------');
                 EmailMessage mes = new EmailMessage();
                  //mes.CcAddress = envelope.ccAddresses;
                  mes.ToAddress = envelope.toAddress;
                  mes.FromName = email.FromName;
                  mes.FromAddress = envelope.fromAddress;
                  //mes.RelatedTo = email.subject.substringAfter('Service Inquiry');
                  mes.HtmlBody = email.plainTextbody;
                  mes.Subject = email.subject;
                  
                  insert mes;
                  //to save attachment to activity
                  //for(Messaging.Inboundemail.BinaryAttachment bAttachment:
                  //email.binaryAttachments) {
                      //Attachment attachment = new Attachment();
                      //attachment.Name = bAttachment.fileName;
                      //attachment.Body = bAttachment.body;
                      //attachment.ParentId = mes.Id;
                      //insert attachment;
                  //}               
                
                  result.success = true;
            }catch (Exception e){
                  result.success = false;
                  result.message = 'Oops, I failed.' + e.getMessage();
            }
              return result;
      }
      return null;
 
  }
}

 
AakashSidanaAakashSidana
I have the same query, nobody answered this one. kindly help