0

I have created a email service wherein I have to check if the email body has the salesforce Id in it and If yes I have to get that Id.

I will always have Id in this format:

[Shipment] : **********

[Order] : ********

[Opty] : ***********

I will have these tags followed by the Ids in my email body.

The email body may have any text in it.

Please suggest!

closed as off-topic by Eric, Mark Pond, Adrian Larson, Sebastian Kessel, Ratan Paul Sep 10 '16 at 2:55

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – Eric, Mark Pond, Adrian Larson, Sebastian Kessel, Ratan Paul
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    This question doesn't show anything that you've attempted to implement for yourself for us to assist you with. One avenue you could attempt would be to use the Pattern & Matcher classes to parse the body of the email for ID values using a Regular Expression pattern similar to this one, which matches on a 15 or 18 character ID for the User object (005). 005[0-9a-zA-Z]{12,15} – Mark Pond Sep 10 '16 at 0:00
  • I have actually found a solution for this. I am using a delimiter to separate my tag and SFDCID . I have used some functions from string class to achieve this. Thank you all for your suggestions!! – Shweta Soparkar Sep 11 '16 at 0:52
4

I've actually written something like this before. Here's a pared down version of what you're looking for:

public Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope env) {
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
    String body = email.plainTextBody == null? (email.htmlBody == null? '': email.htmlBody.stripHtmlTags()): email.plainTextBody;
    Pattern p = Pattern.compile('(\\p{Alnum}{15}\\p{Alnum}{3}?)');
    Matcher m = p.matcher(body);
    while(m.find())) {
        try {
            Id recordId = (Id)m.group(0);
            // Do something with the ID value
        } catch(Exception e) {
            // Not a valid Id after all...
        }
    }
    result.success = true;
    return result;
}

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