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 Content document link

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. And it should be done without giving modify all permission.

Could You please help me giving Delete Access to the Other Users..

  • Is that goal that you want to give delete access to a set of users? Or that you just want to delete the original ContentDocumentLink record once you've assigned the new CDL to the user in question? You're saying the former, but your code seems to say the latter.
    – pchittum
    Sep 16, 2021 at 8:31
  • @pchittum yes i need to give delete access to a set of users. so, first I've implemented a trigger to share the file with collaborator permission, and then implemented a trigger to delete the file from files after removing file from the record Sep 17, 2021 at 6:54


You must log in to answer this question.

Browse other questions tagged .