2

I have pretty simple requirement but I am facing error and I can't understand what is going wrong.

I need to add ContentDocumentLink to add file to CollaborationGroup after insert with C permission. I am holding CollaborateGroup Id in SomeCustomSetting__c to avoid hardcoding Id in TriggerHandlerClass.

This is my Trigger Code:

trigger ContentDocumentTrigger on ContentDocument (after insert) {
    if (Trigger.isInsert && Trigger.isAfter) {
        ContentDocumentTriggerHandler.afterInsert(Trigger.new);
    } 
}

And this is TriggerHandler Code:

public with sharing class ContentDocumentTriggerHandler {
    public static void afterInsert(List<ContentDocument> contentDocuments) {
        String collaborationGroupId = String.valueOf(TakedaSettings__c.getInstance().FileGroupId__c);
        List<ContentDocumentLink> newLinks = new List<ContentDocumentLink>();

        for (ContentDocument document : contentDocuments) {
            newLinks.add(new ContentDocumentLink(
                ContentDocumentId = document.Id,
                LinkedEntityId = collaborationGroupId,
                ShareType = 'C',
                Visibility = 'AllUsers'
            ));
        }

        System.debug(JSON.serializePretty(newLinks));

        try {
            insert newLinks;
        } catch (Exception e) {
            System.debug('$$ Error: ' + e.getMessage());
        }
    }
}

I have also tried

Id collaborationGroupId = (Id) String.valueOf(SomeCustomSetting__c.getInstance().FileGroupId__c);

This is error from logs after inserting some ContentDocument files:

DEBUG|$$ Error: Insert failed. First exception on row 0; first error:
INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

Of course in debug with serialized object, all fields are filled correctly.

The weirdest thing is, if I am doing insert from Execute Anonymous in Developer Console, the record is created correctly. Don't understand what I am doing wrong. Thanks for all your responses.

3
  • The error may be due to a mismatch between the ContentDocumentId and the LinkedEntityId in the ContentDocumentLink object. You need to make sure that the ContentDocumentId refers to a valid file that exists in the org, and that the LinkedEntityId refers to a valid collaboration group that you have access to. You can check the IDs of the files and the groups by going to the Files and Groups tabs in Salesforce. Commented Nov 23, 2023 at 9:44
  • @TusharJadav all ids are correct. LinkedEntityId is copied from record into custom settings field and content document id is from After insert triggered content document so I am sure that Ids are correct.
    – Maciej
    Commented Nov 23, 2023 at 9:49
  • Have you tried using a ContentVersion trigger (checking for VersionNumber=1)? Commented Nov 23, 2023 at 10:45

1 Answer 1

1

What I did in the following is: direct retrieval of the FileGroupId__c from SomeCustomSetting__c as an Id, eliminating unnecessary type casting. Also added, null checks are introduced to ensure the collaborationGroupId is not null before proceeding with the creation of ContentDocumentLink records. Enhanced error handling is implemented with try-catch blocks for both retrieving the custom setting value and inserting new links. hopefully this helps

Trigger updated

trigger ContentDocumentTrigger on ContentDocument (after insert) {
    if (Trigger.isInsert && Trigger.isAfter) {
        ContentDocumentTriggerHandler.afterInsert(Trigger.new);
    } 
}

Trigger handler updated code

public with sharing class ContentDocumentTriggerHandler {
    public static void afterInsert(List<ContentDocument> contentDocuments) {
        Id collaborationGroupId;
        try {
            collaborationGroupId = SomeCustomSetting__c.getInstance().FileGroupId__c;
        } catch (Exception e) {
            System.debug('Error retrieving Collaboration Group ID: ' + e.getMessage());
            return;
        }

        if (collaborationGroupId == null) {
            System.debug('Collaboration Group ID is null');
            return;
        }

        List<ContentDocumentLink> newLinks = new List<ContentDocumentLink>();
        for (ContentDocument document : contentDocuments) {
            newLinks.add(new ContentDocumentLink(
                ContentDocumentId = document.Id,
                LinkedEntityId = collaborationGroupId,
                ShareType = 'C',
                Visibility = 'AllUsers'
            ));
        }

        try {
            insert newLinks;
        } catch (Exception e) {
            System.debug('Error inserting new ContentDocumentLinks: ' + e.getMessage());
        }
    }
}

You must log in to answer this question.

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