[SalesForce] Scraping text from email and storing in a field

I know that there is a web to lead form I can create to do something similar, but is there an easy way to get the inbound email address which can be monitored and the data from the email can be attached to a field in a custom object?

In short

User emails —> salesforce@customemail.com –> pick the content in the email body –> store it in account's description field

The whole idea is that the EMT's do not want to log into SF to update a description field and all they want to do is something similar to what approval process have. Email approve, reject and the status is updated on the approval process
Thanks in advance for any pointers

Best Answer

Email Services are one way to do this. Given the following example, you'd be able to send an email whose subject is the Account Id, body is the Account Name, and it would update accordingly:

  1. create a new Apex class "AccountUpdateEmailHandler" containing this code:

    global class AccountUpdateEmailHandler implements Messaging.InboundEmailHandler {
      global Messaging.InboundEmailResult handleInboundEmail(
      Messaging.InboundEmail email,
      Messaging.InboundEnvelope envelope)
      {
        String subject = email.subject;
        Pattern idPattern = Pattern.compile('001[A-Za-z0-9]{12}');
        Matcher matcher = idPattern.matcher(subject);
        if (!matcher.find()) System.assert(false, 'No Account Id in subject!');
    
        Account account = [SELECT Name FROM Account WHERE Id = :matcher.group(0)];
        account.Name = email.plainTextBody;
        update account;
    
        Messaging.InboundEmailresult result = new Messaging.InboundEmailResult();
        result.message = 'Account Name is now ' + account.Name;
        return result;
      }
    }
    
  2. from Setup > Develop > Email Services, create one using the above class and Activate it,

  3. create an "AccountUpdate" Service Address, configure "Accept Email From" and try sending to it,

Some explanation:

  • up top, the regular expression extracts the Account Id string like 001000000000abc,
  • we update the Name field as required, using the email body,
  • optionally email a response back to you so that you know what was updated,
Related Topic