9

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.: []

0

2 Answers 2

21

Rather than querying ContentDocumentId from ContentVersion, query that from ContentDocument and use that as contentlink.ContentDocumentId=documents[0].Id;

List<ContentDocument> documents = [
    SELECT Id, Title, LatestPublishedVersionId 
    FROM ContentDocument
];

ContentDocumentLink contentlink = new ContentDocumentLink();
contentlink.LinkedEntityId = availableRateDeck.Id;
contentlink.ShareType = 'C';
contentlink.LinkedEntityId = availableRateDeck.Id; 
contentlink.ContentDocumentId = documents[0].Id;
contentlink.Visibility = 'AllUsers'; 
insert contentlink;

Just to let you know, following piece of code works perfectly for me

Account acct = new Account(Name = 'TEST_ACCT');
insert acct;

ContentVersion contentVersion = new ContentVersion(
  Title = 'Penguins',
  PathOnClient = 'Penguins.jpg',
  VersionData = Blob.valueOf('Test Content'),
  IsMajorVersion = true
);
insert contentVersion;    
List<ContentDocument> documents = [
    SELECT Id, Title, LatestPublishedVersionId 
    FROM ContentDocument
];

//create ContentDocumentLink  record 
ContentDocumentLink cdl = new ContentDocumentLink();
cdl.LinkedEntityId = acct.id;
cdl.ContentDocumentId = documents[0].Id;
cdl.ShareType = 'V';
insert cdl;
0
1

Try setting the FirstPublishLocationId to the specified location you want to attach the Content Version first.

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

If memory serves me right. If you do not set a first published location id, it will publish the file in the Personal Files folder, which is only accessible by the user, as opposed to record access. If you need additional objects you want to share the file with, then create a new ContentDocumentLink record.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .