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
ExploreForceExploreForce 

CONTENTDOCUMENTLINK: FIELD_INTEGRITY_EXCEPTION, You cannot create a link for a document in a personal library: [ContentDocumentId]

Hi,

I have class for which I am facing small issue to cover my test class:

             //My controller class where I am creating ContentDocumntLink

             ContentDocumentLink newFileShare = new ContentDocumentLink();
                newFileShare.ContentDocumentId = listsfa[0].Secured_Files__r.Content_Document_Id__c;
                newFileShare.LinkedEntityId = fileSt.User__c;
                if(fileSt.Requested_Access_Type__c == 'Read')
                     newFileShare.ShareType= 'V';
                else if(fileSt.Requested_Access_Type__c == 'Read/Write')
                    newFileShare.ShareType= 'C';
                listContentShares.add(newFileShare);
                insert listContentShares;


   ***************************************************************************************  

   //TEST CLASS Coverage code

        string before = 'Testing base 64 encode';            
         Blob beforeblob = Blob.valueOf(before);
         //Insert contentdocument data
         ContentVersion cv = new ContentVersion();
         cv.title = 'test content trigger';      
         cv.PathOnClient ='test';           
         cv.VersionData =beforeblob;          
         insert cv;         
                                                
         ContentVersion testContent = [SELECT id, ContentDocumentId FROM ContentVersion where Id = :cv.Id];

        ContentDocumentLink newFileShare = new ContentDocumentLink();
        newFileShare.contentdocumentid = testcontent.contentdocumentid;
        newFileShare.LinkedEntityId = fileSt.User__c;
        newFileShare.ShareType= 'V';
        insert newFileShare;


     In the first attemp I tried with the above SQL query and I m getting below error:

     System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, You cannot create a link for a document in a personal library: [ContentDocumentId]

 
To resolve the above error: I have come up with solution ,

       ContentWorkspace testWorkspace = [SELECT Id FROM ContentWorkspace limit 1];
         ContentWorkspaceDoc newWorkspaceDoc =new ContentWorkspaceDoc();
         newWorkspaceDoc.ContentWorkspaceId = testWorkspace.Id;
         newWorkspaceDoc.ContentDocumentId = testContent.ContentDocumentId;
         insert newWorkspaceDoc;

  It's throwing error:
   System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, You do not have the level of access necessary to perform the operation you requested. Please contact the owner of the record or your administrator if access is necessary.: [ContentWorkspaceId]



Please help!!
Best Answer chosen by ExploreForce
ExploreForceExploreForce
This resolved my issue.

Solution:

string before = 'Testing base 64 encode';            
         Blob beforeblob = Blob.valueOf(before);
        
          
           
         //Insert contentdocument data
         ContentVersion cv = new ContentVersion();
         cv.title = 'test content trigger';      
         cv.PathOnClient ='test';           
         cv.VersionData =beforeblob;          
         insert cv;         
                                                
         ContentVersion testContent = [SELECT id, ContentDocumentId FROM ContentVersion where Id = :cv.Id];
  
         FeedItem feeditem = new FeedItem();
        feedItem.Type = 'ContentPost';
        feedItem.Body = 'Place holder file. Upload attachmenet using Upload new version.';
        feedItem.ContentData = beforeblob;
        feedItem.ContentFileName = 'Test File';
        feedItem.ParentId = sfa.id;
        insert feedItem;
           
        List<ContentDocumentLink > ctd = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :sfa.Id];
        if(ctd.size() > 0)
          newSecFile.Content_Document_Id__c = ctd[0].ContentDocumentId;
      update newSecFile;
         
//insert new ContentDocumentLink
        ContentDocumentLink newFileShare = new ContentDocumentLink();
        newFileShare.contentdocumentid = testcontent.contentdocumentid;
        newFileShare.LinkedEntityId = fileSt.User__c;
        newFileShare.ShareType= 'V';

All Answers

Vinit_KumarVinit_Kumar
You seem to be querying the contentworkspace id of org,however I don't see @IsTest(SeeAllaData=true) in your test class.

Another thing make sure you have the access to Conten Workspace which you are trying to query.

Hope this helps !!
ExploreForceExploreForce
Thank you for your reply. 

I have set @IsTest(SeeAllaData=true) in my test class.
I will check whether I have access to Content Workspace.
Thanks.
ExploreForceExploreForce
Is there any other solution for this?


ExploreForceExploreForce
This resolved my issue.

Solution:

string before = 'Testing base 64 encode';            
         Blob beforeblob = Blob.valueOf(before);
        
          
           
         //Insert contentdocument data
         ContentVersion cv = new ContentVersion();
         cv.title = 'test content trigger';      
         cv.PathOnClient ='test';           
         cv.VersionData =beforeblob;          
         insert cv;         
                                                
         ContentVersion testContent = [SELECT id, ContentDocumentId FROM ContentVersion where Id = :cv.Id];
  
         FeedItem feeditem = new FeedItem();
        feedItem.Type = 'ContentPost';
        feedItem.Body = 'Place holder file. Upload attachmenet using Upload new version.';
        feedItem.ContentData = beforeblob;
        feedItem.ContentFileName = 'Test File';
        feedItem.ParentId = sfa.id;
        insert feedItem;
           
        List<ContentDocumentLink > ctd = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :sfa.Id];
        if(ctd.size() > 0)
          newSecFile.Content_Document_Id__c = ctd[0].ContentDocumentId;
      update newSecFile;
         
//insert new ContentDocumentLink
        ContentDocumentLink newFileShare = new ContentDocumentLink();
        newFileShare.contentdocumentid = testcontent.contentdocumentid;
        newFileShare.LinkedEntityId = fileSt.User__c;
        newFileShare.ShareType= 'V';
This was selected as the best answer
Shiva Prasad Achukatla 22Shiva Prasad Achukatla 22
Hello, Below is the test class that I have written for contentdocumentlink trigger. And its failing to insert the cdl due to insufficent access. 

@IsTest
public class TestAttachmentHandler 
{
    static testMethod void validateHelloWorld()
    {
        
        
        Master_Rate_Deck_XRef__c availableRateDeck = new Master_Rate_Deck_XRef__c(Deck_Type__c = 'Wholesale', RateDeckType__c = 'Std.', Billing_Term_Rate_Type__c = 'LRN');
        insert availableRateDeck;
        Blob beforeblob=Blob.valueOf('Unit Test Attachment Body');

        ContentVersion cv = new ContentVersion();
        cv.title = 'test content trigger';      
        cv.PathOnClient ='test';           
        cv.VersionData =beforeblob;          
        insert cv;         

        ContentVersion testContent = [SELECT id, ContentDocumentId FROM ContentVersion where Id = :cv.Id];
        
        
        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=availableRateDeck.id;
        contentlink.ShareType= 'C';
        contentlink.LinkedEntityId = availableRateDeck.Id; 
        contentlink.ContentDocumentId=testcontent.ContentDocumentId;
        contentlink.Visibility = 'AllUsers'; 
        insert contentlink
    }
}
Error:System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_OR_READONLY, You do not have the level of access necessary to perform the operation you requested. Please contact the owner of the record or your administrator if access is necessary.: []