2

I have written an inbound email service to created records based on custom object. This email service processes replies to the outbound email sent from Salesforce. The outbound email's template and letterhead contains the logos. So when the email service runs it returns the following logos' URLs in the textbody.

From: "[email protected]" Sent: Monday, August 27, 2018 9:35 PM To: Jacks, Roger (VISPL) Subject: [EXT] Sandbox: ** Case #: 1114193

[https://domain.cs92.my.salesforce.com/servlet/servlet.ImageServer?id=015q00000007e50&oid=00Dq00000001Kt0]

Hi Team,

This is regarding email body validation.

Thanks & Regards, Roger Jacks

[https://domain.cs92.my.salesforce.com/servlet/servlet.ImageServer?id=015q00000007e4q&oid=00Dq00000001Kt0]

[http://domain.cs92.my.salesforce.com/servlet/servlet.ImageServer?oid=00Dq00000001Kt0&esid=018q0000000vqVR]

ref:_00Dq01Kt0._500q0BJQKJ:ref

How can I remove the image addresses from the TextBody on the Messaging.inboundEmail object ? I tried following code but my RegEx expression is not working.

String RegEx = '\\bmy.salesforce.com/servlet/servlet.ImageServer?id=\\b';

 Pattern MyPattern = Pattern.compile(RegEx);
 Matcher MyMatcher = MyPattern.matcher(email.plainTextBody);
 String replacedBody;

if(myMatcher.find()) {
   replacedBody = email.plainTextBody.replaceAll(RegEx, '');
}

I'm struggling to develop a RegEx expression. My !dea is to find those 3 instances of the image URLs. The common string among them is .my.salesforce.com/servlet/servlet.ImageServer?id=

4
  • What code have you written so far, and which bit doesn't seem to work?
    – Nick C
    Sep 3, 2018 at 6:10
  • Bennie, could you post the code you use to generate that email? One more thing: be aware that you are including an image loaded in your salesforce org, so it won' be accessible from external sources (e.g. emails). You should upload these images somewhere public.
    – Alberto
    Sep 3, 2018 at 11:22
  • @NickCook I have added what I have written so far. It is RegEx which is not working.
    – Jarvis
    Sep 3, 2018 at 15:28
  • @Alberto Basically, I'm struggling to develop a RegEx expression. My !dea is to find those 3 instances of the image URLs. The common string among them is .my.salesforce.com/servlet/servlet.ImageServer?id=
    – Jarvis
    Sep 3, 2018 at 16:03

1 Answer 1

3
+50

Since you are trying to find a specific pattern within a block of text, use regular expression. String.replaceAll() method is perfect for your situation.

String emailBody = '[https://domain.cs92.my.salesforce.com/servlet/servlet.ImageServer?id=015q00000007e50&oid=00Dq00000001Kt0] Hi Team, This is regarding email body validation. Thanks & Regards, Roger Jacks [https://domain.cs92.my.salesforce.com/servlet/servlet.ImageServer?id=015q00000007e4q&oid=00Dq00000001Kt0] [http://domain.cs92.my.salesforce.com/servlet/servlet.ImageServer?oid=00Dq00000001Kt0&esid=018q0000000vqVR]';

String regExp = '\\[(https?)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[my.salesforce.com/servlet/servlet.ImageServer?id=]*[-a-zA-Z0-9+&@#/%=~_|]\\]';
String withoutUrls = emailBody.replaceAll(regExp, '');

System.debug('Email without urls: ' + withoutUrls);
// Result: Hi Team, This is regarding email body validation. Thanks & Regards, Roger Jacks  

Apex regular expression is based on Java (source).

For Java you can find a lot of examples and real-time environments that will make creation of regular expression a lot easier.

I've used RegEx Planet.

2

You must log in to answer this question.

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