4000 character limit on Case object - debugging a Trigger issue

What was the problem?

You may or may not know that there is currently (as of Winter 16) a 4,000 character limit on the Case Comments field on the case object.



4,000 characters can be very quickly filled up when dealing with complex queries, or if you set up a process to copy the content of your email to case responses into the case comments (a very common customisation).

I recently encountered an issue where a Trigger set up to copy any new email to case into a case comment. Here's the background and eventual fix.

You're not alone

If you have ever come across this limit, don't worry. You are certainly not alone in hoping that Salesforce increases this. Here is a small selection of the Success requests asking for this:

https://success.salesforce.com/answers?id=90630000000hS1IAAU
https://success.salesforce.com/ideaview?id=08730000000Bq7lAAC
https://success.salesforce.com/answers?id=90630000000hS1IAAU
https://success.salesforce.com/answers?id=90630000000gxHkAAI


Deploying a Trigger to copy email to case

In the section below I will discuss a new trigger that we set up to copy all new emails into case comments. However I later found a great posting from Marco (werewolf) Casalaina where he kindly offered a Trigger and Test Class to copy the comments automatically.

This would have saved me a lot of time so here's a link for anyone else with this challenge.


Trigger implemented, but long emails not being added

Knowing that we had to keep the body of the case comments to 4,000 I set up my Trigger to only copy up to the first 3,999 chraracters of an email submited.

I tested my Trigger and everything seemed to be working perfectly at first.

However, after sending a few longer emails it quickly became aparrant that any email over 4,000 was not be copied to case comments at all.

//find out if the message is longer the 4000 chacters
//if so cut it down to max 4000
   
System.debug('Email length is ' + theTextLimitedEmailMessage.length());
if(theTextLimitedEmailMessage.length() > 3999){

aNewComment.CommentBody = theTextLimitedEmailMessage.left(3500);
//aNewComment.CommentBody = 'Long message was ' + theTextLimitedEmailMessage.length();
} else {
//aNewComment.CommentBody = 'Message was ' + theTextLimitedEmailMessage.length() + ' and the message ' + theTextLimitedEmailMessage;
aNewComment.CommentBody = theTextLimitedEmailMessage;

Debugging

The logic of the code seemed correct so to help debug I queued up several emails of differing lengths. I then added a bunch of debug statements to the code to find out why I was pushing through the 4,000 character limit.

After a lot of trial and error, I discovered that the trigger was not including the email footer text when counting the characters.

So if my email was 2,000 characters, the trigger would treat it as a short message and attempt to copy the whole text into a case comment including the signature (another 200 characters). The final character count was 2,200 characters.

However, if my email contained 3,900 characters, again it was treated as a "short" email and the trigger attempted to copy the body into a case comment. However, when the signature was added the character count went to 4,100 and record saving failed.

Also, if I sent an email over 5,000 characters the trigger was taking the first 3,999 characters then inserting that into the case comment. Again, the addition of the email signature pushed it to over 4,000 and the record failed again.


Solution

I amended the Trigger to only include a maximum of 3,500 characters - job done.

Here's an extract of the final (working) version.

String theTextLimitedEmailMessage = anEmail.TextBody;
   
//find out if the message is longer the 4000 chacters
//if so cut it down to max 4000
   
System.debug('Email length is ' + theTextLimitedEmailMessage.length());
if(theTextLimitedEmailMessage.length() > 3999){
    
aNewComment.CommentBody = theTextLimitedEmailMessage.left(3500);
//aNewComment.CommentBody = 'Long message was ' + theTextLimitedEmailMessage.length();
   
} else {

//aNewComment.CommentBody = 'Message was ' + theTextLimitedEmailMessage.length() + ' and the message ' + theTextLimitedEmailMessage;
aNewComment.CommentBody = theTextLimitedEmailMessage;
}

Conclusion in a line - 4,000 characters don't include automatic email footers.

Comments