Directly with a codeunit is not the approach to take. Success has been achieved by using a custom table to import the detail. Once done a codeunit is then used to iterate through the table records and use a add link function. Something like this:
Trigger OnRun()
Var
CustomNotes: Record CustomNotes;
Contact: Record Contact;
begin
GetLastEntryNo();
if NotesImport.FindSet() then
repeat
if Contact.Get(NotesImport."Contact No.") then begin
CreateSystemLink(Contact.RecordId, NotesImport.Note);
NotesImport.Delete();
end;
until NotesImport.Next() = 0;
end;
procedure CreateSystemLink(IncomingRecordID: RecordId; NoteText: Text)
var
RecordLink: Record "Record Link";
LinkManagement: Codeunit "Record Link Management";
begin
LastEntryNo += 1;
RecordLink.Init();
RecordLink.Company := CompanyName();
RecordLink.Type := RecordLink.Type::Note;
RecordLink.Created := CurrentDateTime;
RecordLink."User ID" := UserId();
RecordLink."Link ID" := LastEntryNo;
RecordLink."Record ID" := IncomingRecordID;
LinkManagement.WriteNote(RecordLink, NoteText);
RecordLink.Insert(true);
end;
procedure GetLastEntryNo()
var
RecordLink: Record "Record Link";
begin
if RecordLink.FindLast() then
LastEntryNo := RecordLink."Link ID"
else
LastEntryNo := 0;
end;
var
LastEntryNo: Integer;
}