+ Start a Discussion
Anto HotelbedsAnto Hotelbeds 

Email handler with chinese characters

Hi,

I have an email handler for my EmailToCase. I am having errors when receiving an email with chinese characters.

What I do in my handler is take the body of the email, cut it to 4000char (max length of a case comment) and insert it as a case comment in the case. This is my handler code thats inserts the new case comment:

newCommand.CommentBody=newCommand.CommentBody+'\n\t\n'+CommentEmail;
               system.debug('Longitud del comment: '+newCommand.CommentBody.length());
               newCommand.CommentBody=limitLength(newCommand.CommentBody,CaseComment.CommentBody.getDescribe().getLength()-1).unescapeHtml4();
               system.debug('Comment length after cutting: '+newCommand.CommentBody.length());
             newCommand.IsPublished = TRUE;
             newCommand.ParentId =case.Id;
            insert newCommand;

When I make a debug, I receive that the length of the case comment I am about to insert is 3798, but I still get the error:

error: STRING_TOO_LONG, Body: data value too large:........... (max length=4000): [CommentBody]

This just happens with emails sent with chinese characters.

Did anyone face this issue before? Anyone has an idea on how to fix this?

Thanks a lot,

Antonio
NehalNehal (Salesforce Developers) 
Hi,

In API versions previous to 15.0, if you specify a value for a field that is one of the datatypes listed, and that value is too large, the value is truncated. For API version 15.0 and later, if a value is specified that is too large, the operation fails and the fault code STRING_TOO_LONG is returned. AllowFieldTruncationHeader allows you to specify that the previous behavior, truncation, to be used instead of the new behavior in API versions 15.0 and later. This header has no effect in versions 14.0 and earlier.  Please refer to link below:

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_header_allowfieldtruncation.htm

Also, using trim method :

Trim :
Returns a copy of the string that no longer contains any leading or trailing white space characters.
Leading and trailing ASCII control characters such as tabs and newline characters are also removed. White space and control characters that aren’t at the beginning or end of the sentence aren’t removed.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_string.htm

Further for your reference, please go through links below:

1.https://developer.salesforce.com/forums/ForumsMain?id=906F000000098cjIAA
2.https://success.salesforce.com/answers?id=90630000000gu6sAAA
3.http://www.opfocus.com/blog/5-things-that-could-give-your-custom-salesforce-apex-code-hiccups/

I hope this helps.