I have written a trigger class on content document link to create a task whenever a community user creates a task on a case. Currently, the trigger works perfectly when attaching a case through the UI, but I cannot seem to get a unit test written that will work.

What I am trying to do is insert a contentdocumentLink in the unit test relating to the user, as the trigger is expecting a contentdocumentlink whose linked entity ID is a user to check the profile to see if it is a portal user or not. However, despite setting up the ContentDocumentLink as similarly to the one created when doing a UI insert as I can, I am getting an insufficient cross reference ID error on insert.

Here is the test class-

@isTest public class LinkTest {

public static testMethod void test1(){

    Case c = new Case();
    insert c;

    Account testAccount = new Account();
    Contact testContact = new Contact();

    // ACCOUNT 

    testAccount.name = 'PUCO Community upload';
    insert testAccount;

    // CONTACT 
    testContact.firstName = 'First';
    testContact.lastName = 'Last';
    testContact.Email = 'standarduser@testorg123456.com';
    testContact.AccountId = testAccount.id;
    testContact.MailingStreet = 'Street 1';
    testContact.MailingCity = 'Columbus';
    testContact.MailingState = 'Ohio';
    testContact.MailingPostalCode = '43215';
    testContact.Phone = '999-999-9999';
    insert testContact;

    Id pid = [select id from profile where name ='Community Plus Login User'].id;

    User testUser = new User(ContactId = testContact.id, Alias = 'COMM', Email='standarduser@testorg.com', 
        EmailEncodingKey='UTF-8', LastName='kkTesting', LanguageLocaleKey='en_US', 
        LocaleSidKey='en_US', ProfileId =pid,TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg123456.com');
    insert testUser;

    ContentVersion cv=new Contentversion();
    cv.PathOnClient ='test';
    Blob b=Blob.valueOf('Unit Test Attachment Body');
    cv.versiondata=EncodingUtil.base64Decode('Unit Test Attachment Body');
    cv.FirstPublishLocationId =  c.Id; 
    insert cv;

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

    System.debug('insertedDoc = ' + insertedDoc);

    ContentDocumentLink l1 = new ContentDocumentLink();
    l1.ContentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;
    l1.LinkedEntityId = testUser.id;
    l1.ShareType = 'V';
    System.debug('linkedId = ' + l1.LinkedEntityId);
    insert l1;



The failure is on insert on line holding insert l1, and is outputting 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.: [LinkedEntityId]

Any idea what I may be doing wrong or another way to simulate this file upload for testing purposes?

  • Have you tried doing this in a System.runAs block as the test user? – Charles T Nov 28 '19 at 0:51
  • I was writing answer a while ago that did that... but I tried it out on my system and it seems it's still not working. Perhaps using the connect api is the answer? – Caspar Harmer Nov 28 '19 at 5:27
  • 1
    I think the answer is: You don't need to. When you insert the original file, you get two CDL records created - one linking the file to the case and one linking the user to the file. Ie:[ { "ContentDocumentId": "0691D000000SeGZQA0", "LinkedEntityId": "005d0000001TbiAAAS", "ShareType": "I", "Visibility": "AllUsers", "Id": "06A1D0000011VcuUAE" }, { "ContentDocumentId": "0691D000000SeGZQA0", "LinkedEntityId": "5001D000003Ogb8QAC", "ShareType": "V", "Visibility": "InternalUsers", "Id": "06A1D0000011VcvUAE" } ]. So it's already done! – Caspar Harmer Nov 28 '19 at 6:48
  • @CasparHarmer you seem to be correct that the links are already creating on insertion of the contentversion, so creating them does seem to be unnecessary. Unfortunately, when I attempt to do a system run as with the test user, I get a new error stating that they do not have access rights on the NetworkId field. Certainly progress but a new hurdle to clear here. Thanks for your help in getting this moving in the right direction. – Frank Evers Nov 29 '19 at 16:15

The possible reasons are:-

  • Internal Users will not be able to share files with community Users if the files are attached to the 'Files' related list. Since, you have related the file to a Case record, It goes and sit in case record file related list. As a workaround, Use Chatter Publisher to share files with Community Users. Read more here:- Share object files with community Users

To resolve your issue, In your apex code, You can insert a record feed with file to share it with the community user.

Note: By default, files uploaded by Internal Users are only visible internally while files uploaded by community Users are visible to 'All with access'.

Also read this:- Community User cannot view 'Files' migrated from 'Attachments'

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.