+ Start a Discussion
Yao Lin 8Yao Lin 8 

Inbound Email Service: update a field

Hello, 
I am working on inbound email service to update a field in Salesforce. 
When I receive an email from a specific email address, I need to look for the email content to see if it contains words "FULL OUT". If it does, I need to find the serial number on the email content to match a record Container (API: Container__c) in Salesforce. Then update Status field (picklist; API: Status__c) on that Container record. How to write the Apex code to look for the words? Please see the example email below: 
From: xxx@gmail.com

Service FULL OUT was performed for shipments listed below
ABCDEF123: (this is the serial number and a container name in Salesforce) I need to find this serial number in email and see if a container record is created in Salesforce. 
Size: 12
Destination: NY
Delivery Date: 01/18/2018
 
Best Answer chosen by Yao Lin 8
srlawr uksrlawr uk
Firstly, you will need some way to identify where the code is in the e-mail, in my example below, I take the 9 characters following the first '#' in the e-mail so you will need to get the body of the email to read:

Service FULL OUT was performed for shipments listed below
#ABCDEF123:
Size: 12

(or come up with another way to substring the email body)


You can then write a trigger on insert of an EmailMessage perhaps, here is an example:

(this took ages to write up, and will be FULL of syntax errors, please don't copy and paste it into Salesforce and expect to save it, please use it as a basis for learning the final answer!)
 
trigger ProcessEmail on EmailMessage (before insert) {

     // list of emails that meet problem
      Map<Id, String> messages = new Map<Id, String>();

    for(EmailMessage thismail : trigger.new) {

       if(thismail.TextBody.contains('FULL OUT') {

         // something like this will find the 9 characters following the first # in the email
         String codey = thismail.TextBody.suibstring(thismail.TextBody.indexOf('#'), thismail.TextBody.indexOf('#') + 9);

           messages.put(thismail.Id, codey);
 
       }

    }

   // now process those messages
    if(messages.ketSet().size() > 0) {
            // load the related object
           List<Container__c> conts = [SELECT Id FROM Container__c WHERE Code__c IN :messages.values()];
          for(Container__c thisCont : conts) {
             thisCont.Status__c = 'DUFF';
          }
         update conts;
    }


}



 

All Answers

srlawr uksrlawr uk
Firstly, you will need some way to identify where the code is in the e-mail, in my example below, I take the 9 characters following the first '#' in the e-mail so you will need to get the body of the email to read:

Service FULL OUT was performed for shipments listed below
#ABCDEF123:
Size: 12

(or come up with another way to substring the email body)


You can then write a trigger on insert of an EmailMessage perhaps, here is an example:

(this took ages to write up, and will be FULL of syntax errors, please don't copy and paste it into Salesforce and expect to save it, please use it as a basis for learning the final answer!)
 
trigger ProcessEmail on EmailMessage (before insert) {

     // list of emails that meet problem
      Map<Id, String> messages = new Map<Id, String>();

    for(EmailMessage thismail : trigger.new) {

       if(thismail.TextBody.contains('FULL OUT') {

         // something like this will find the 9 characters following the first # in the email
         String codey = thismail.TextBody.suibstring(thismail.TextBody.indexOf('#'), thismail.TextBody.indexOf('#') + 9);

           messages.put(thismail.Id, codey);
 
       }

    }

   // now process those messages
    if(messages.ketSet().size() > 0) {
            // load the related object
           List<Container__c> conts = [SELECT Id FROM Container__c WHERE Code__c IN :messages.values()];
          for(Container__c thisCont : conts) {
             thisCont.Status__c = 'DUFF';
          }
         update conts;
    }


}



 
This was selected as the best answer
srlawr uksrlawr uk
oh shoot, you mentioned inbound e-mail services, very good point...!!!

Well, you can do a very similar thing in Apex in your inbound email service, of course!
Yao Lin 8Yao Lin 8
Thank you so much, srlawr! It gave me some clues how to look for words in the email content. I will work on it! Again, I appreciate it! Definitely the best answer!
Asams TJAsams TJ
I also have a similar situaton. I have an email service on case.If a keyword is found in email plauintextbody,it should change case status to closed.
This is my code for inbound email class. Can you tell me where I go wrong?
global class CaseSMEmailHandler implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {
        
    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

    String search = 'processed';
Case vCon = [SELECT CW_Case_Status__c FROM Case WHERE Id =: email.subject];
     if(email.plainTextBody.contains(search))
     {
       try{
         vCon.CW_Case_Status__c = 'completed';
         update vCon ;
         result.success = true;
       }
       catch(DmlException e)
        {
            result.success=false;
            System.debug('The following error has occurred: ' + e.getMessage());
        }
     }
     return result;
   }
}
 
Gabe Sanchez 7Gabe Sanchez 7
I also have a similar situation. We have a vendor that sends us an email with the this in the body : Ref:MSG44096150. 

I need to pull this number from the email body and update a case field. Any ideas where to start?