+ Start a Discussion
StenderStender 

Inbound Email Service Not Recognizing String in Email Body

Hello,

 

I have a custom email service that is used to track/update some things on cases.  I am having an issue though when parsing text from the email body.  There is a specific string I am looking for in the body that will cause certain updates to happen.  I have the code debug the email body and the two strings I am looking for are there, but the code acts like it is incorrect.

 

The frustrating thing is that this only happens when the email is sent from the original source.  I have a copy of the email and when I forward the message for testing reasons it recognizes the string just fine.  Here is the code for this piece:

string emailType;        
string emailStart = email.plainTextBody.left(325).normalizeSpace();
string initialResponseString1a = 'Thank you for approaching RateGain Customer Support';
string initialResponseString1b = 'We have received your message and logged it in our automated tracking system under the';                
system.debug('---------------------------------> emailStart:  ' + emailStart);
if(emailStart.contains(initialResponseString1a) &&  emailStart.contains(initialResponseString1b)){
    emailType = 'initial response';
}else{emailtype = 'update';}
system.debug('---------------------------------> emailType:  ' + emailtype);

 Here is the debug log of when this is from the original source:

4:00:13.217 (1217711000)|USER_DEBUG|[51]|DEBUG|---------------------------------> emailStart:  Thank you for approaching RateGain Customer Support.  We have received your message and logged it in our automated tracking system under the Ticket # 1029941. This ticket is now being assigned to a Resolution Expert who will analyze this and revert within 4 hours.  _________________________________________________________
14:00:13.217 (1217725000)|SYSTEM_METHOD_EXIT|[51]|System.debug(ANY)
14:00:13.217 (1217753000)|SYSTEM_METHOD_ENTRY|[57]|System.debug(ANY)
14:00:13.217 (1217770000)|USER_DEBUG|[57]|DEBUG|---------------------------------> emailType:  update

 

 

As you can see BOTH strings I am looking for are in there, but the code is still saying emailType = update.  However, when I send the email from my email address the debug says this:

14:24:56.206 (206699000)|USER_DEBUG|[51]|DEBUG|---------------------------------> emailStart:  Thank you for approaching RateGain Customer Support. We have received your message and logged it in our automated tracking system under the Ticket # 1029854. This ticket is now being assigned to a Resolution Expert who will analyze this and revert within 4 hours. ____________________________________________________________
14:24:56.206 (206722000)|SYSTEM_METHOD_EXIT|[51]|System.debug(ANY)
14:24:56.206 (206766000)|SYSTEM_METHOD_ENTRY|[57]|System.debug(ANY)
14:24:56.206 (206809000)|USER_DEBUG|[57]|DEBUG|---------------------------------> emailType:  initial response
14:24:56.206 (206825000)|SYSTEM_METHOD_EXIT|[57]|System.debug(ANY)

 

 

How is this possible.  Is it some sort of formatting with the email from the originating system?  How can I handle that in APEX?

 

Thanks,
Jeremy Stender

 

Best Answer chosen by Admin (Salesforce Developers) 
crop1645crop1645

Jeremy -- I feel your pain as this has happened to me and left me baffled.. Exact same symptoms. I put hex debugging of the string into my code to see what was different between the origin system and forwarded-via-my-email-account

 

What is different is that the mail from remote system A goes straight to SFDC inbound email handler whereas your test actually copies the mail from remote system A to your email system B before it is forwarded by you to SFDC.

 

I would be looking at hex displays of the inbound string that vary between the two use cases. Perhaps the distinction between   and space or some of the other wacky UTF-8 space characters.

All Answers

crop1645crop1645

Jeremy -- I feel your pain as this has happened to me and left me baffled.. Exact same symptoms. I put hex debugging of the string into my code to see what was different between the origin system and forwarded-via-my-email-account

 

What is different is that the mail from remote system A goes straight to SFDC inbound email handler whereas your test actually copies the mail from remote system A to your email system B before it is forwarded by you to SFDC.

 

I would be looking at hex displays of the inbound string that vary between the two use cases. Perhaps the distinction between   and space or some of the other wacky UTF-8 space characters.

This was selected as the best answer
StenderStender

Hi Eric,

 

That was exactly the issue.  I eventually ran some .deleteWhiteSpace() methods on the strings and the email body and then debugged them and eventually found exactly that, a non-standard space.  Once I determined WHERE that space was, I was able to just 'chop up' the strings AROUND that character and everything worked just fine.

 

Thanks,
Jeremy Stender

crop1645crop1645

Terrific!  You may also encounter in the future some issues between dash and em - they look almost the same typographically but are different chars.  'Em' appears a lot in web pages where someone then does copy-paste into an SFDC field but code is looking for a dash.

 

Anyway, please mark as solution