I've been stuck with this trigger for 2 days now. Essentially the trigger will convert any attachments that are generated in the Agreement Object to Salesforce Files. However upon generating attachments this specific error keeps getting thrown. "Object reference not set to an instance of an object"

public class CNG_AttachmentstoFileTriggerHandler {
    public static void afterInsert(List<Attachment> attachinserted) {
        Set<ID> agreementset = New Set<ID> ();
        List<ID> attachedIDs = New List<ID> ();
        for (Attachment attach: attachinserted) {
            if (attach.ParentId.getSobjectType() == Apttus__APTS_Agreement__c.SobjectType) {
        //Holds agreements with new Attachments      
        List<Apttus__APTS_Agreement__c > agreementAUS = [Select Id FROM Apttus__APTS_Agreement__c
            WHERE CNG_AUS_RecordType__c = TRUE AND ID IN: agreementset

        //Holds attachments from correct Recordtypes
        List<Attachment> attachtoupdate = [SELECT Id, ParentId, Name, IsPrivate, Body, OwnerId, CreatedById, CreatedDate, Description FROM Attachment
            WHERE ParentId IN: agreementAUS AND ID IN: attachedIDs

        //Conversion of Attachments into ContentVersions                

        //map Attachment Id with ContentVersion record
        Map<Id, ContentVersion> attachmentCVs = new Map<Id, ContentVersion> ();
        //Map —> Attachment – Attachment Parent Id 
        Map<Id, Id> attachmentParentIds = new Map<Id, Id> ();

        //Generating file version using content version object
        if (!attachtoupdate.isEmpty()) {
            for (Attachment att: attachtoupdate) {
                ContentVersion cv = new ContentVersion();
                cv.ContentLocation = 'S';
                cv.PathOnClient = att.Name;
                cv.Origin = 'H';
                cv.OwnerId = att.OwnerId;
                cv.Title = att.Name;
                cv.VersionData = att.Body;
                cv.FirstPublishLocationId = att.ParentId;
                attachmentCVs.put(att.Id, cv);
                attachmentParentIds.put(att.Id, att.ParentId);

        //Insert the content versions from attachment data
        if (attachmentCVs.values().size() > 0) {
            insert attachmentCVs.values();

        //to map ContentVersionId with AttachmentParent Id for ContentDocument Linking
        map<Id, Id> cvToAttachmentParentIdMap = new map<Id, Id> ();

        List<Id> cvIds = new List<Id> ();
        for (Id key: attachmentCVs.keySet()) {
            ContentVersion cv = attachmentCVs.get(key);
            Id attachmentParentId = attachmentParentIds.get(key);
            cvToAttachmentParentIdMap.put(cv.Id, attachmentParentId);
        //Fetching contentDocumentId using contentVersionId

        List<ContentVersion> cvWithDocIds = [select Id, ContentDocumentId from ContentVersion where Id IN: cvIds];

        //to link the files to its parent records
        List<ContentDocumentLink> contentDocumentLinks = new List<ContentDocumentLink> ();
        for (ContentVersion cv: cvWithDocIds) {
            Id attachmentParentId = cvToAttachmentParentIdMap.get(cv.Id);
            ContentDocumentLink cl = new ContentDocumentLink(LinkedEntityId = attachmentParentId, ContentDocumentId = cv.ContentDocumentId, ShareType = 'V');

        if (contentDocumentLinks.size() > 0) {
            insert contentDocumentLinks;

  • ADDING: This is an after insert trigger Handler on Attachment
    – Kevin S
    Jun 9 at 17:19
  • Does it give a line number where the error is being thrown? Jun 9 at 17:26
  • @NicholasSavini System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Document with ID: 0692D000000jOke is already linked with the entity with ID: a0E2D000001mvav: Linked Entity ID: [LinkedEntityId] ..........................on Line 74........................
    – Kevin S
    Jun 10 at 8:45
  • @NicholasSavini didn't notice the logs. But i'm not sure what causes this error
    – Kevin S
    Jun 10 at 8:49

UPDATE: the ContentDocumentLink wasn't required as there was something in play that already linked the content version to the object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.