Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I am facing the issue on the case comment size. There is an inbound email handler which creates a case comment on a specific case record based on the incoming email body.

Sometimes its giving error - "STRING_TOO_LONG, Body: data value too large". Then i checked the limit for case comment body and its 4000 bytes and not characters.

Now the problem is how do i check for size of a string as bytes and not as characters in apex? I found that string .size() method returns number of characters and not no of bytes.

Please help.

Thanks

share|improve this question

2 Answers 2

The documentation for String.length() (there is no String.size()) explains that it:

Returns the number of 16-bit Unicode characters contained in the String.

So the number of 8 bit bytes is 2x the length of the string. (And so to fix the problem you need to truncate at 2000 characters.)

PS Looks like Blob.valueOf(stringValue).size() is a better way to go - see that answer.

share|improve this answer
    
Actually, your conversion doesn't work. Truncating to 2000 characters is safe, but you are probably chopping off a lot more than you need. For example, I have a string from an email body which is 3999 characters. If I convert it to a blob and the check the size, it is 4015. This won't fit. If I shorten it a bit more so the blob size is less than 4000, it fits. –  Daniel Hoechst Sep 24 at 21:23
    
@DanielHoechst I guess the question here is the encoding: UTF-8 with mostly single byte characters will occupy not many more bytes that the size in characters whereas UTF-16 (Unicode) will occupy x2. It appears to be possible to assign a String directly to CaseComment.CommentBody - is Blob relevant here? Or is using Blob a way to get the number of bytes in UTF-8 format? –  Keith C Sep 24 at 21:42

Use Blob.valueOf(stringValue).size() to get the size in bytes. It all depends on what characters are sent in the email that determines the size in bytes.

The following method worked for me to abbreviate the string in bytes:

private String byteAbbreviate(String stringToAbbreviate, Integer byteLength) {
    //First abbrevate using the character length. This will get us close
    stringToAbbreviate = stringToAbbreviate.abbreviate(byteLength);
    Integer abbLength = byteLength;
    while (Blob.valueOf(stringToAbbreviate).size() > byteLength) {
      abbLength = abbLength - 1;
      stringToAbbreviate = stringToAbbreviate.abbreviate(abbLength);
    }
    return stringToAbbreviate;
}

You can use DMLOptions to truncate the CaseComment when it is inserted.

Database.DMLOptions dmo = new Database.DMLOptions();
dmo.allowFieldTruncation = true;
Database.insert(comment, dmo);
share|improve this answer
    
Is the Blob Class documentation for size wrong when it says "Returns the number of characters in the Blob" rather than the number of bytes? –  Keith C Sep 24 at 21:44
    
I think so. My testing seems to be if the Blob size of the string is less than 4k, it will go into CaseComments. If it is greater, it won't fit. –  Daniel Hoechst Sep 24 at 22:17
    
Simple enough then. +1 from me. –  Keith C Sep 25 at 7:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.