I have an inbound email service that parses through emails and fills in the relevant fields in salesforce records- including a field identifying which team completed the work.

The email contains the email address (with hyperlink) of an individual. I've written my apex code to identify the team by extracting the individual's name from the email address in the email text body and then matching the individual's name to the team.

The code works when the inbound email has the hyperlink removed (when I manually remove the hyperlink and forward it to salesforce during testing), but not when the hyperlink is there (when emails are automatically forwarded to salesforce- how it will work in production). Debug log shows the name looks correct with both versions of the email sent in. I've used stripHtml tags- is there something else I'm missing to convert to plain text and remove hyperlinks?

Example Email Text: Work Completed by: [email protected] on 01/31/2022

Extract of code:

String emailBody = '';
String Person = '';
String Team1 = 'super.man;bat.man';
String Team2 = 'spider.man;silver.surfer';
String Team = ''; 

String backslash = '\n';
String backslashReplacement = '';

//Add the email text into the local variable     
//crop email if forwarded (removes header)
        emailBody=email.htmlBody.replaceAll(backslash, backslashReplacement);

//strip Html tags        

Person = emailBody.substringBetween('by:', '@');
Person = Person.deleteWhitespace();

if (Team1.indexOf(Person)>=0) {
   Team = 'Team1';
 else if (Team2.indexOf(Person)>=0){
  Team = 'Team2';
else {                          
   Team = 'Other';
  • What's the actual html look like? Is it possible you have the word 'Subject' somewhere in the email after the 'Work Completed by:' part?
    – Nick C
    Commented Feb 11, 2022 at 3:38
  • I don't think so?--- <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"> </head> <body> <p><span style="font-size: 16px">Work </span><span style="color: rgb(65,168,95); font-size: 16px"><strong>COMPLETED</strong></span><span style="font-size: 16px"> by:</span><span style="font-size: 16px"><strong> &nbsp;</strong></span><span style="font-size: 16px"><strong>[email protected]</strong></span><span style="font-size: 16px"><strong> </strong></span><span style="font-size: 16px">on </span><span style="font-size: 16px">02/13/2022</span>
    – LXerson
    Commented Feb 13, 2022 at 22:53


You must log in to answer this question.