Salesforce.com InboundEmailHandler - unresolved exceptions
I'm currently having problems with my InboundEmailHandler on Salesforce. Currently we save all emails sent through the EmailHandler to an object called a communication log, which is linked between contacts and accounts. The communication log contains all the information of the original email, including to/from/cc, subject, body, attachments, ect. There is code in place to truncate the length of the data being input into the fields, but in some strange cases exceptions are being thrown reporting that the max length (32000), of the RTF field (body) in the communication log has been exceeded. I've been scratching my head for a couple days trying to figure this one out.
Here's where the fields are truncated: string truncatedBody = GetTruncatedString( messageLog.Body_c, 32000 ); messageLog.Body_c = truncatedBody;
Here's the truncation method: public string GetTruncatedString(string currentValue, integer maxLength) { string truncatedString = null;
if(currentValue != null)
{
if(currentValue.length() > maxLength)
开发者_StackOverflow社区{
truncatedString = currentValue.substring(0,maxLength - 1);
}
else
{
truncatedString = currentValue;
}
}
return truncatedString;
}
The docs are wrong on this, the limit is not 32000 characters, but 32000 bytes, the string is stored in utf-8, so if you have a 32000 character string, it will be over the limit if you have any characters in the string that require 2 more bytes to be stored in utf-8. You can use the blob object to obtain the number of bytes required to store the string.
Integer numBytes = Blob.valueOf(s).size();
You'll probably want to truncate to 32000 chars, then start truncating additional characters based on the number of bytes you're over the limit.
精彩评论