function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Brandon WhiteBrandon White 

Max length applied to Rich Text Fields

I recently encountered an error (400 error code: STRING_TOO_LONG) when attempting to update a rich text field through the REST API. I was passing some markup to this field that included a literal '&' ampersand character, which is encoded in HTML as '&'. In my case, the field had a maxlength limit of 256 characters (the lowest threshold you can place on a rich text field at the moment).

I thought that the literal '&' character would only count as 1 character against the maxlength limit but it seems that Salesforce considers this '&' character to count as 5 characters in its encoded representation. This is similar for other types of encoded characters such as copyright ('&copy'). If the '&' counts as 5 characters against the limit, then I can understand why I received the STRING_TOO_LONG error.

I did check and this behavior is consistent in the Salesforce Web UI.

I don't think this behavior is necessarily intuitive, particularly if you're not sure how certain characters are encoded behind the scenes. If you're working with a rich text field that has a max length of 256 characters, it's easy to craft some text that, at least visually, looks to be under the max length limit. But in practice, because the max length is applied against the encoded characters, the total length of the text exceeds the max length limit.
SandhyaSandhya (Salesforce Developers) 
Hi,

Some special characters encode into HTML upon input to a RTA field, even when copied from a neutral editor.  For example, a single apostrophe will encode to the equivalent of 5 characters, resulting in a net gain of 4 characters.  This should be taken into account when considering field length settings and character limits.

As per the below salesforce help document.

https://help.salesforce.com/HTViewSolution?id=000002749&language=en_US
 
Please mark it as Best Answer if my reply was helpful. It will make it available for other as the proper solution.
 
Thanks and Regards
Sandhya

 
Brandon WhiteBrandon White
Hi Sandhya:

Thanks for the reference. The apolstrophe is yet another example of a character that is encoded with its associating HTML escape sequence behind the scenes.

Do you know if there is documentation available that describes all of the character substitutions that will take place for a RTA field?

thanks,
Brandon