0

I am new to Email Service concepts in salesforce. Please help me out.

I am working in Email to Case. When someone send email from outside to salesforce then new case record should be created which set some of its fields and reply back to user with case id . I am able to achieve it. Code is here:

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;
  }
}

Now what i want is user again reply back to same email then emailservice should handle this reply and set case comments related list of case record with body of reply.

Thanks in advance..!!

1 Answer 1

1

When Case is created you send auto response with case Id. Instead of sending case Id in auto response send case number. because it's hard to identify Id from string subject by regex. and use below code

global class CreateCase implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
    String caseNumber;
    Pattern p = Pattern.compile('\\d{8}+');
    Matcher m = p.matcher(email.Subject);
    while(m.find()){
        caseNumber = m.group();
    }
    if(caseNumber == null) {
        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 { //If subject contains case number create case comment instead of a new case.
        Case caseObj = [ SELECT Id, CaseNumber FROM Case WHERE CaseNumber = :caseNumber];
        CaseComment caseCommentObj = new CaseComment(ParentId = caseObj.Id, CommentBody= email.Subject +'\n'+ email.plaintextbody);
        insert caseCommentObj;
        //result.message='Thanks for sending mail. email is attached to case comment'+caseObj.Id;
    }

   return result;
  }
}
15
  • When mail came from salesforce it appears to come from my gmail account which i have linked with that org and in via some salesforce domain is written. But when i am replying back to the mail to update case comment object. It is replying on my gmail and nort able to receive case comment body. Commented Jan 28, 2019 at 5:04
  • You can start auto forwarding from your gmail(Email which you are using for auto response) to Email service email in salesforce. Commented Jan 28, 2019 at 8:33
  • Not getting can you please elaborate why code is not receiving case comment record...what is the need of gmail forwarding Commented Jan 28, 2019 at 8:48
  • Email will come into salesforce only when it is sent to Email service created in salesforce. You can find that email from setup --> Email Services--> click on email service which you are using. At the bottom there will be a long email address. copy that email and start auto forwarding from your gmail to that email address. Commented Jan 28, 2019 at 8:52
  • In my requirement if user replied to the mail then only case comment record is created then why to use auto forwarding from gmail. Also it is asking for some verification code Commented Jan 28, 2019 at 9:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .