I'm trying to create ContentNote records from Note ones but it's always giving me this particular error:
FATAL_ERROR System.UnexpectedException: Note can't be saved
Code:
List<Note> noteLIST = [SELECT Id, Title, Body, OwnerId, Owner.Profile.Name, ParentId, IsPrivate, CreatedById, CreatedDate FROM Note WHERE Owner.Profile.Name LIKE 'current_profile_i_want_to_check'];
List<ContentNote> cnLIST = new List<ContentNote>();
for(Note n : noteLIST){
ContentNote cn = new ContentNote(Title = n.Title,
Content = Blob.valueOf(n.Body));
cnLIST.add(cn);
}
if(!cnLIST.isEmpty()){
insert cnLIST;
}
I've checked this post: Help with "System.UnexpectedException: Note can't be saved" in batch apex but still not working.
Best Answer
I'm the author of the
ContentNote
library referred to in the linked answer. I'll reiterate the things I'm aware of that you need to do to prepare note content for insertion:<>"'&
) with their corresponding entities (&
and friends).<br>
(taking care with Windows CRLF/Linux LF/Mac CR)'
with'
.'
, result in an exception. Unicode should be left as the bare characters.Also note that
Here's how I actually prepare notes there:
If you don't precisely meet the expectations, you'll get an
UnexpectedException
, which is uncatchable and unhandleable in the same way as aLimitException
.Existing App
You may also want to look at Doug Ayers' existing application for converting classic Notes into ContentNotes.