Hi,
We have to create 2 object records :
- ContentNote - For Creating ContentNote Record
- ContentDocumentLink - For linking to the Record (ex: Account, Contact etc)
ContentNote Record creation:
The following Apex code creates a note and escapes any special characters so they’re converted to their HTML equivalents.
ContentNote cnObj = new ContentNote();
cnObj.Title = 'test1';
String body = 'Hello World. Before insert/update,
escape special characters such as ", ', &,
and other standard escape characters.';
cnObj.Content = Blob.valueOf(body.escapeHTML4());
insert cnObj;
The following code creates a note using text that is already formatted as HTML, so it doesn’t need to be escaped.
ContentNote cnObj = new ContentNote();
cnObj.Title = 'test2';
String body = '<b>Hello World. Because this text is already
formatted as HTML,
it does not need to be escaped.
Special characters such as ", etc.
must already use their HTML equivalents.
</b>';
cnObj.Content = body;
insert cnObj;
To Link to a record (eg: Account Record Id = 001XXXXXXXXXXXXXXX) :
ContentDocumentLink cdlObj = new ContentDocumentLink();
cdlObj.ContentDocumentId = cnObj.Id;
cdlObj.LinkedEntityId = '001XXXXXXXXXXXXXXX';//We have to add record id //dynamically here based on requirement
insert cdlObj;
Reference: