Whenever a Content Document is Uploaded on a record, I need to Share the file to a group of users and give delete Access to the same users.

I've Implemented a trigger to share the Document With the Users. The Users getting access to remove the File from the record.

Here is my trigger on ContentDocumentLink:

trigger ShareAndDeleteFiles on ContentDocumentLink (After insert, After Update) {
    If(trigger.IsInsert && trigger.IsAfter){
        System.debug('Calling Share files Method');
    If(trigger.IsUpdate && trigger.IsAfter){
        System.debug('Calling Delete files Method');
        FilesShareAndDeleteHandler.deleteFiles(trigger.oldmap, trigger.newMap);


Here is my trigger helper class:

public class FilesShareAndDeleteHandler{
    public static void shareFiles(List<ContentDocumentLink> cdlList1){
       Set<Id> documentIdsForUsr = new Set<Id>();
        Set<Id> newDocumentIds = new Set<Id>();
        List<ContentDocumentLink> cdlList = new List<ContentDocumentLink>();
        id userID = '0055g00000CKLykAAH';
        List<ContentDocumentLink> cdl=[SELECT id,LinkedEntityId,ContentDocument.title,ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId =:userID];  
        for(ContentDocumentLink cdLink:cdl){  
        for(ContentDocumentLink cdLink:cdlList1){  

for(ID id : newDocumentIds){
                ContentDocumentLink cd = new ContentDocumentLink();
                cd.ContentDocumentId = id;
                cd.LinkedEntityId = userID;
                cd.ShareType = 'C';
                cd.Visibility = 'allusers';
               system.debug('New Assignment Is being Created');
       insert cdlList;
    public static void deleteFiles(map<id,ContentDocumentLink> oldMapFiles,map<id,ContentDocumentLink> newMapFiles){
        set<id> linkedIds=new set<id>();
        list<ContentDocumentLink> filesToDelete =new list<ContentDocumentLink>();
        list<ContentDocumentLink> newCdl=new list<ContentDocumentLink>([select contentDocumentId,contentDocument.publishStatus from contentDocumentLink where contentDocumentId in: oldMapFiles.keyset()]);
        for(ContentDocumentLink cdl:oldMapFiles.values()){
            if(oldMapFiles.get(cdl.id).contentDocument.publishStatus=='P'&& newMapFiles.get(cdl.id).contentDocument.publishStatus=='R'){
        filesToDelete =[ select contentDocumentId,contentDocument.publishStatus from contentDocumentLink where contentDocumentId in:linkedIds];
        delete filesToDelete;

But after Removing, the file Remains in the Files Object.

Note: Removing Files from the record(Case) is not even triggering as an Update event on both Files object and case object.

How do I give delete access to other users?

  • 1
    Welcome to Salesforce Stack Exchange (SFSE). Please take the Tour & look in the Help Center - especially What topics can I ask about here? where it states, "Questions that ask for the community to write code or provide step-by-step instructions for implementation...are likely to be closed." This site exists to help you. Please edit your question to include what you have tried (code) & how it is not working (e.g., exact errors, issues).
    – Moonpie
    Sep 15 at 11:25

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.