Similar to Rick's answer, I once wrote a trigger to do exactly this.
First, create an object (I named it Note_Creation_Trigger), with the following fields
- Content__c (Long Text Area), representing the contents of the note
- Title__c (Text), representing the title of the note
- Owner_Id__c (Text or User Lookup), representing the Id of the user owner
- Related_Record_Id__c (Text), representing the record that note should reside in
- Share_Type__c (Text), can be I, V or C. See https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentdocumentlink.htm (search ShareType)
Then add the following Trigger
trigger NoteCreationTrigger on Note_Creation_Trigger__c (before insert, before update, after insert, after update) {
if (Trigger.isInsert && Trigger.isAfter) {
List<ContentNote> notes = new List<ContentNote>();
List<ContentDocumentLink> links = new List<ContentDocumentLink>();
Map<Id, ContentNote> notesById = new Map<Id, ContentNote>();
for(Note_Creation_Trigger__c triggerNote : Trigger.new){
ContentNote note = new ContentNote();
note.Content = Blob.valueof(triggerNote.Content__c);
note.Title = triggerNote.Title__c;
note.OwnerId = triggerNote.Owner_Id__c;
notes.add(note);
notesById.put(triggerNote.Id, note);
}
if(notes.size()>0){
insert notes;
}
for(Note_Creation_Trigger__c triggerNote : Trigger.new){
ContentDocumentLink documentLink = new ContentDocumentLink();
documentLink.LinkedEntityId = triggerNote.Related_Record_Id__c;
documentLink.ContentDocumentId = notesById.get(triggerNote.Id).Id;
documentLink.ShareType = triggerNote.Share_Type__c != null ? triggerNote.Share_Type__c : 'I';
links.add(documentLink);
}
if(links.size()>0){
insert links;
}
}
}
Import the notes into this object and all should be good.
EDIT: @user1669296 requested that I post the unit test. See code below. It is just to pass code coverage (provides 100% coverage).
@IsTest
private class NoteCreationTest {
static testMethod void coverageTest() {
Account a = new Account(Name = 'Test Account');
insert a;
insert new Note_Creation_Trigger__c(
Content__c = 'Content',
Title__c = 'Title',
Related_Record_Id__c = a.Id,
Owner_Id__c = UserInfo.getUserId()
);
}
}