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
Linda 98Linda 98 

Inbound email service to capture reply and link to the record

I am having a Custom object which sends an email(Process builder does that) to the contact field on the record with a template.

I am trying to 
1. When the contact email clicks reply and fill the template, the response should be returned to the record and linked to the same record.
2. Parse the values in the template and update the record from which email is sent.
I am sure I have to use the inbound email service class but not able to figure on how it works and how to start with.
I had already read on email services and inbound email services but couldn't understand how can I implement that my particular req.

 
Tad Aalgaard 3Tad Aalgaard 3

You will need to create an Email Service in Setup that points to your new class.

Here is some code that I wrote a while back to process emails and parse out the body.  It then calls another class and method in which additional work is completed.  There should be enough in the code below to give you an idea to get you started.
 

global class  ApprovalResponseFromEmail implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
        
        System.debug('RECEIVED EMAIL');
        
        String fromAddress = email.fromAddress;
        System.debug('fromAddress: ' + fromAddress);        
        
        String plainTextBody = email.plainTextBody;
        System.debug(plainTextBody);

        String subject = email.subject;
        System.debug(subject);

        // pull the Approval Response Entry Id from the email subject

        String AreId = '';
        if(subject.contains('[')){
            AreId = subject.substring(subject.lastIndexOf('[')+1,subject.lastIndexOf(']')); 
        }

        System.debug('AreId: ' + AreId);
        
        // pull the status from the email body
        String status = '';
        if(plainTextBody.contains('[Approved]')){
            status = 'Approved';
        }else if(plainTextBody.contains('[Rejected]')){
            status = 'Rejected';
        }

        System.debug('status: ' + status);

        // pull the comments from the email body
        Integer startIndex = plainTextBody.indexOf('Please add any comments below this line.');
        Integer endIndex = plainTextBody.indexOf('Please add any comments above this line.');
        
        System.debug(startIndex);
        System.debug(endIndex);

        String comments = plainTextBody.subString(startIndex+41,endIndex).trim().replaceAll('\n+', '\n');
        
        System.debug('comments: ' + comments);

        // only update the ARE if the email contained a status (Approved or Rejected)
        if(status != ''){
            ApprovalProcess ap = new ApprovalProcess();
            ap.updateARE(AreId, status, comments, fromAddress); 
        }

        return result;
    }
    
}
Tad Aalgaard 3Tad Aalgaard 3
Turn on logging in your Salesforce org. If you get an email it should show the actions of the class in the log. You can also query the EmailMessage records to see if it came in.
Tad Aalgaard 3Tad Aalgaard 3
When you say handler are you referring to Apex code as the handler?  Or are you referring to Pardot form handlers? 

I need more information in the way of code or screen shots, or something more.
Tad Aalgaard 3Tad Aalgaard 3
Go to Setup and check if check your Email Service Address is set to "Accept Email From" just one address.  If it is, uncheck it or add your yahoo email.