0

When I upload a file via the chatter feed I want that the file is shared with a particular group in my org with contributor status. I think it must be a trigger on the ContentDocumentLink and a after insert trigger but how do I change the sharing settings of the given file?

Edit Here is my code:

trigger publishFileToGroup on ContentDocumentLink (after insert) {

    List<CollaborationGroup> chattergroup = [SELECT Id FROMCollaborationGroup WHERE Name = 'Apex Trigger Group'];  
    List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];

    ContentDocumentLink cdl = New ContentDocumentLink(

    LinkedEntityId = chattergroup[0].Id, ContentDocumentId = documents[0].Id, shareType = 'C');

    insert cdl;
}

Which creates this error message:

Apex trigger publishFileToGroup caused an unexpected exception, contact your administrator: publishFileToGroup: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, publishFileToGroup: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Document with ID: 06958000000Aejh is already linked with the entity with ID: 0F9580000005tul: Linked Entity ID: [LinkedEntityId]

  • 2
    Can share your code whatever you have tried so far – Santanu Boral Aug 12 '17 at 21:45
  • I'm pretty certain you're going to want to be sharing ContentDocumentVersion, NOT ContentDocumentLink to the ContentDocument. – crmprogdev Aug 13 '17 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.