简体   繁体   English

Salesforce.com InboundEmailHandler-未解决的异常

[英]Salesforce.com InboundEmailHandler - unresolved exceptions

I'm currently having problems with my InboundEmailHandler on Salesforce. 我目前在Salesforce上的InboundEmailHandler遇到问题。 Currently we save all emails sent through the EmailHandler to an object called a communication log, which is linked between contacts and accounts. 当前,我们将通过EmailHandler发送的所有电子邮件保存到称为通讯日志的对象,该对象在联系人和客户之间链接。 The communication log contains all the information of the original email, including to/from/cc, subject, body, attachments, ect. 通信日志包含原始电子邮件的所有信息,包括“至/来自/ cc”,“主题”,“正文”,“附件”等。 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. 有适当的代码可以截断输入到字段中的数据的长度,但是在某些奇怪的情况下,会抛出异常,报告已超出通信日志中RTF字段(正文)的最大长度(32000) 。 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 ); 这是字段被截断的地方:字符串truncatedBody = GetTruncatedString(messageLog.Body_ c,32000); messageLog.Body _c = truncatedBody; messageLog.Body _c = truncatedBody;

Here's the truncation method: public string GetTruncatedString(string currentValue, integer maxLength) { string truncatedString = null; 这是截断方法:public string GetTruncatedString(string currentValue,integer maxLength){string truncatedString = null;

if(currentValue != null)
{
    if(currentValue.length() > maxLength)
    {
        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. 文档对此有误,限制不是32000个字符,而是32000个字节,该字符串存储在utf-8中,因此,如果您有32000个字符串,那么如果字符串中有任何字符,它将超出限制需要将另外2个字节存储在utf-8中。 You can use the blob object to obtain the number of bytes required to store the string. 您可以使用blob对象获取存储字符串所需的字节数。

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. 您可能希望将其截断为32000个字符,然后根据超出限制的字节数开始截断其他字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM