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!

  • 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, 2016 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!! Sep 11, 2016 at 0:52

1 Answer 1


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;

This site is temporarily in read-only mode and not accepting new answers.

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