1

I am working on email to case. My requirement is whenever a user is sending mail from outside to salesforce generated email address, case should be created and reply back to user with case id in subject. I am able to achieve

Now what I want is when user replies back to received email the body of the email should become the body of related list case comments of that case.

Is this requirement can be achieved using thread id concept?

Below is the code I have written so far

    global class CreateCase implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();


        Case caseObj= new Case();
        caseObj.Subject=email.subject;
        caseObj.Description=email.plaintextbody;
        caseObj.Status='Open';  

        QueueSObject QueueID = [Select Queue.Id, Queue.Name, Queue.Type from QueueSObject WHERE 
                                Queue.Name ='Case Open Queue' Limit 1];
        caseObj.OwnerId=QueueID.Queue.Id;

        insert caseObj;


       result.message='Thanks for sending mail. Case is successfully created and the case id is:'+caseObj.Id;

   return result;
  }
}
0

When someone replies to the email, the subject will be same. Lets use this to our advantage.

In your email service, check if the subject contains the caseId, if caseId does not exist means that we have to create a new case. else if caseId exists, means we have to create a case comment.

   global class CreateCase implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();



        String caseId = CreateCase.getCaseId(email.subject);
        if(caseId==null){ // its a new Case
            Case caseObj= new Case();
            caseObj.Subject=email.subject;
            caseObj.Description=email.plaintextbody;
            caseObj.Status='Open';  

            QueueSObject QueueID = [Select Queue.Id, Queue.Name, Queue.Type from QueueSObject WHERE 
                                    Queue.Name ='Case Open Queue' Limit 1];
            caseObj.OwnerId=QueueID.Queue.Id;

            insert caseObj;
            result.message='Thanks for sending mail. Case is successfully created and the case id is:'+caseObj.Id;

        }else{
            //Its a case comment
            CaseComment cc = new CaseComment(ParentId = caseId,CommentBody = email.plaintextbody);
            insert cc;
            result.message=email.subject;


        }




   return result;
  }

  //Utility method to get caseId from subject
  private static String getCaseId(String subject){
    try{
        List<String> splitString = subject.split(':');
        if(!splitString.isEmpty() && splitString.size() == 2){
            return ID.valueOf(splitString[1]);
        }
        return null;
    }catch(Exception e){
        return null;
    }

  }
}
2
  • Isn't the thread id in the form: ref:XXXXXX.XXXXXXXXX:ref? Or are you saying the email template that sends the email must include the record id in the subject? What happens when it's a reply and there's an extra colon in the subject line? e.g. RE: my subject – Nick Cook Feb 1 '19 at 3:31
  • @NickCook threadId is for email-to-case. What you have is apex email service, it doesnt care about threadId – Pranay Jaiswal Feb 1 '19 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.