Tuesday, October 15, 2019

How to Create Content Note using Apex in salesforce

Approach: 1

Use FileExtension as  '.sonte' when creating the Content Note using ContentVersion Object.

Ex:
ContentVersion objCntNote = new ContentVersion();
objCntNote.Title = 'Test Content Note';
objCntNote.PathOnClient = objCntNote.Title + '.snote';
objCntNote.VersionData = Blob.valueOf('This is Test Content Note');
objCntNote.FirstPublishLocationId = '001G000002Baxlj';  // ParentId
insert objCntNote;
System.debug('cntNote Id == > '+objCntNote);

Result

Approach: 2

Use ContentNote object to create Content Note

Ex:
ContentNote objCntNote = new ContentNote();
objCntNote.Title = 'salesforce code crack';
objCntNote.Content = Blob.valueOf('Welcome to salesforcecodecrack.com');
insert objCntNote; //Insert Content Note   

//create ContentDocumentLink  record to link with parentId 
ContentDocumentLink objCntDocLink = new ContentDocumentLink();
objCntDocLink.LinkedEntityId = '001G000002Baxlj'; // Specify your parent Id 
objCntDocLink.ContentDocumentId = objCntNote.Id;  //Content Note Id
objCntDocLink.shareType = 'V'; // Specify your sharing type 
insert objCntDocLink;

Result


1 comment:

  1. One thing to note about the two options... if you are trying to insert ContentVersion with HTML embedded, you will receive the following exception: UNSAFE_HTML_CONTENT, Note can't be saved because it contains HTML tags or unescaped characters that are not allowed in a Note.

    However, inserting ContentNote does not cause this exception (as of API 45, I believe).

    ReplyDelete