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
Mohan N 17Mohan N 17 

Email services for Email-to-case isn't working......

Hi All,
I implemented Email services for Inbound Emails.
When a user sends a mail to company mail case has to be created and that mail has to attached in Email related list,if any attachments consists those need to attach in attachments.
Here everything is going well but my issue is when we reply to mail from salesforce case to user who sends the email mail is going to users mail but when user sends a reply to that mail which is comes from salesforce case the reply is not attaching to salesforce case email related list instead of these it is showing in Activities.
Please help to solve this I'm attaching my code below
<
global class CareServiceClass implements Messaging.InboundEmailHandler 
 {

   global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env)
   {
       Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
       Set<String> Restrictedset = new Set<String>();
        for(Restricted_Keywords__c rr : Restricted_Keywords__c.getall().values())
        {
          Restrictedset.add(rr.Restricted_Keyword_or_Email__c);
         }
       id CaseRectyid = [select id,name,DeveloperName,SobjectType from recordtype where SobjectType = 'Case' and DeveloperName = 'Order_Support'].id;
       id CaseRectyLeadid = [select id,name,DeveloperName,SobjectType from recordtype where SobjectType = 'Case' and DeveloperName = 'Lead'].id;
       id Caseownerid = [select Id from Group where Name = 'IB_EmailToCase team' and Type = 'Queue'].id;
             if(!(Restrictedset.contains(email.fromAddress )  ) )
               {             
                       case c = new case ();
                       c.status= 'Open' ;
                       c.Origin= 'Email' ;
                       c.Priority= 'Email' ;
                     c.Ownerid=Caseownerid ;
                     
                       if(Restrictedset.contains(email.subject))
                       {
                       c.Type = 'Lead';
                       c.Recordtypeid= CaseRectyLeadid;
                       }
                       else{
                       c.Type = 'Order Support';
                       c.Recordtypeid= CaseRectyid ;
                       }
                       c.subject = email.subject;
                       c.Description = email.plainTextBody;
                        
                     //  c.ContactEmail = email.From;
                       insert c;
                       //system.debug('owner'+c);
                       
                     
                    EmailMessage[] newEmail = new EmailMessage[0];
                    newEmail.add(new EmailMessage(FromAddress = email.fromAddress, FromName = email.fromName, ToAddress = email.toAddresses[0], Subject = email.subject,
                         TextBody = email.plainTextBody, HtmlBody = email.htmlBody,ParentId = c.Id,status='0',Incoming=true));   
                    insert newEmail;  
                     
                   List<Attachment> attachmentList = new List<Attachment>();
                      if(email.binaryAttachments != null && !email.binaryAttachments.isEmpty()){
                          system.debug('--- email.binaryAttachments ---'+email.binaryAttachments.size());
                          for(Messaging.InboundEmail.BinaryAttachment bAttach : email.binaryAttachments){
                              Attachment attach = new Attachment();
                              attach.ParentId = c.id;
                              attach.Body = bAttach.body;
                              attach.Name = bAttach.fileName;
                              attachmentList.add(attach);
                          }
                      }
                      
                      if(email.textAttachments != null && !email.textAttachments.isEmpty()){
                         system.debug('--- email.textAttachments ---'+email.textAttachments.size());
                          for(Messaging.InboundEmail.TextAttachment tAttach : email.textAttachments){
                              Attachment attach = new Attachment();
                              attach.ParentId = c.id;
                              attach.Body = Blob.valueOf(tAttach.body);
                              attach.Name = tAttach.fileName;
                              attachmentList.add(attach);
                          }
                      }
                      
                if(!attachmentList.isEmpty())
                          insert attachmentList;
   
                
         }  
         result.success = true;
         return result;
            
     }

}
Vishwajeet kumarVishwajeet kumar
Have you tried to use case Thread ID in email subject while sending or recieving email?
Mohan N 17Mohan N 17
Hi Vishwajeet,
Yes I tried by including threadId in Subject,body using template.