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');
FilesShareAndDeleteHandler.shareFiles(trigger.new);
}
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){
documentIdsForUsr.add(cdLink.ContentDocumentId);
}
for(ContentDocumentLink cdLink:cdlList1){
newDocumentIds.add(cdLink.ContentDocumentId);
}
for(ID id : newDocumentIds){
if(!documentIdsForUsr.contains(id)){
ContentDocumentLink cd = new ContentDocumentLink();
cd.ContentDocumentId = id;
cd.LinkedEntityId = userID;
cd.ShareType = 'C';
cd.Visibility = 'allusers';
cdlList.add(cd);
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'){
linkedIds.add(cdl.id);
}
}
if(linkedIds!=null){
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..
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.