4

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

11

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;
  • Weird I got this error using your code. System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_OR_READONLY, Invalid sharing type C: [ShareType] – compski Dec 10 '19 at 5:27
0

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.

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.