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
David M. ReedDavid M. Reed 

Apex: inserting ContentNote results in exceptions for Unicode characters, lost line breaks

I'm working on a Visualforce page to simplify logging of Activities. As a portion of this process, users are asked to enter supporting text, which is to be attached to the resulting Activity in a note record (I'm using the new ContentNote). What I keep encountering is two issues:
  • In the resulting note, all line breaks are removed: all of the lines are run together with no intervening space.
  • There is an exception thrown repeatedly as follows:
"Note can't be saved because it contains HTML tags or unescaped characters that are not allowed in a Note.
Error is in expression '{!save}' in component <apex:commandButton> in page log_activity: Class.LogActivityController.save: line 123, column 1"

Note that I do use String.escapeHTML4() to prepare the text. Based on my testing the exception appears to be thrown when Unicode characters are present. Am I failing to prepare Unicode text appropriately in some fashion I'm not aware of?

Does anyone have any ideas? 

 
ContentNote n = new ContentNote();
        	
        	if (task.Subject != null && task.Subject != '') {
	        	n.Title = task.Subject;
        	} else {
        		n.Title = 'Activity Notes';
        	}
        	
        	n.Content = Blob.valueOf(longDescription.escapeHtml4());
        	
        	insert n;
        	
        	ContentDocumentLink cdl = new ContentDocumentLink();
        	
        	cdl.ContentDocumentId = n.Id;
        	cdl.LinkedEntityId = task.Id;
        	cdl.Visibility = 'AllUsers';
        	cdl.ShareType = 'I';
        	
        	insert cdl;

 
VineetKumarVineetKumar
David M. ReedDavid M. Reed
@VineetKumar this does help solve the line break problem as follows:
 
n.Content = Blob.valueOf(longDescription.escapeHtml4().replace('\r\n', '<br>').replace('\n', '<br>').replace('\r', '<br>'));

I still cannot get the system to accept Unicode input. Doing .escapeUnicode() doesn't change the behavior.
David M. ReedDavid M. Reed
Okay, resolved this issue. Unicode is supported correctly, however, the method escapeHTML4() actually escapes Unicode characters with HTML entities. E.g. ñ becomes &#xf1; The documentation is a little unclear on this. Since ContentNotes appear to reject all HTML entities other than the basics (ampersand, angle brackets), use String.escapeXML() to prepare text and it works correctly with or without Unicode characters.
David M. ReedDavid M. Reed
This is the code behind my solution: https://github.com/davidmreed/DMRNoteAttachmentImporter
Eric KokmanianEric Kokmanian
Hi,
I have a similar issue and wrote a question which remains unanswered. Could anyone please help me?
Here is the link: https://developer.salesforce.com/forums/ForumsMain?id=9060G0000005i8FQAQ