function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Darshit Pathak 3Darshit Pathak 3 

INVALID_CROSS_REFERENCE_KEY exception in ContentDocumentLink

below is my code in test class in which I am getting the exception.
Can anyone help me with root cause and solution?

ContentVersion cv = new ContentVersion();
        cv.Title = 'Test Content';
        cv.PathOnClient = 'test';
        cv.VersionData = EncodingUtil.base64Decode('Unit Test Attachment Body');
        cv.NetworkId = // customer community network id;
        insert cv;
Id contentDocumentId = [select Id, ContentDocumentId, OwnerId from ContentVersion where Id = :cv.Id].ContentDocumentId; 
      List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument where ID =:contentDocumentId];
      System.assertEquals(documents.size(), 1);
      System.debug('DocId : '+contentDocumentId);
      ContentDocumentLink cdl = New ContentDocumentLink(); 
      cdl.LinkedEntityId = caserec.id; // id of a case record inserted earlier in the method
      cdl.ContentDocumentId = contentDocumentId;
      cdl.shareType = 'V';
      insert cdl;   // getting exception on this line.
Ajay K DubediAjay K Dubedi
Hi Darshit,

Such errors popup only when you try to update a field with wrong values. Therefore please debug caserec.id and contentDocumentId just before updating them to LinkedEntityId and ContentDocumentId  and check wheather record corresponding to these ids exists.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Deepali KulshresthaDeepali Kulshrestha
Hi Darshit,
Greetings to you!

- Please use the below code : -  
ContentVersion cv = new ContentVersion();
    cv.Title = 'Test Content';
    cv.PathOnClient = 'test';
    cv.VersionData = EncodingUtil.base64Decode('Unit Test Attachment Body');
    cv.NetworkId = // customer community network id;
    insert cv;
    
    ContentVersion contentVersionObj = [select Id, ContentDocumentId, OwnerId from ContentVersion where Id = :cv.Id];
    Id contentDocumentId = [select Id, ContentDocumentId, OwnerId from ContentVersion where Id = :cv.Id].ContentDocumentId; 

    List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument where Id =:contentVersionObj.ContentDocumentId];
    
    System.assertEquals(documents.size(), 1);
    System.debug('DocId : '+contentVersionObj.ContentDocumentId);
    ContentDocumentLink cdl = New ContentDocumentLink(); 
    cdl.LinkedEntityId = caserec.id; // id of a case record inserted earlier in the method
    cdl.ContentDocumentId = contentVersionObj.ContentDocumentId;
    cdl.shareType = 'V';
    insert cdl;


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
Darshit Pathak 3Darshit Pathak 3
Thanks Ajay.
System.debug('DocId : '+contentDocumentId); prints the document Id , and caseRec Id is also gets printed .
 
Darshit Pathak 3Darshit Pathak 3
Thanks Deepali,
I tried your way too, still the error is same.
Rucha Pradhan-ThakkarRucha Pradhan-Thakkar
Were you able to find the root cause or workaround for this exception? I am facing the same exception so curious