Last active
May 30, 2020 07:42
-
-
Save arun12209/2c0b3c3eaaf49150d2081e5d3d2e9d85 to your computer and use it in GitHub Desktop.
ContentDocumentLink_Trigger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger ContentDocumentLink_Trigger on ContentDocumentLink (after insert) { | |
if(Trigger.isAfter && Trigger.isInsert){ | |
Set<ID> contentDocIds = new Set<ID>(); | |
for(ContentDocumentLink cdl: Trigger.new){ | |
contentDocIds.add(cdl.ContentDocumentId); | |
} | |
Set<Id> eventIds = new Set<Id>(); | |
Map<String,String> eventMap = new Map<String,String>(); | |
for(ContentDocumentLink cdlObj: Trigger.new) | |
{ | |
if(String.valueOf(cdlObj.LinkedEntityId).startsWith('00U')){ //Check if file is uploaded on event object | |
eventIds.add(cdlObj.LinkedEntityId); | |
} | |
} | |
for(Event e:[select id,whatId from Event where Id IN:eventIds]){ | |
eventMap.put(e.Id, e.whatId); | |
} | |
Map<String,String> parentRecMap = new Map<String,String>(); | |
for(ContentDocumentLink cdl: Trigger.new){ | |
if(String.valueOf(cdl.LinkedEntityId).startsWith('00U')){ //Check if file is uploaded on event object | |
if(String.valueOf(eventMap.get(cdl.LinkedEntityId)).startsWith('001')) //Check if event parent is Account | |
parentRecMap.put(cdl.ContentDocumentId, eventMap.get(cdl.LinkedEntityId)); //Map<ContentDocumentId,AccountId> | |
} | |
} | |
List<ContentDocumentLink> cdl_List = new List<ContentDocumentLink>(); | |
if(ContentDocumentLink_TriggerHelper.Flag == true){ | |
ContentDocumentLink_TriggerHelper.Flag = false; | |
for(String str: parentRecMap.keySet()){ | |
ContentDocumentLink cdl = new ContentDocumentLink(); // Content Document Link to share the file with Account(Parent) record | |
cdl.LinkedEntityId = parentRecMap.get(str); // Account ID | |
cdl.ContentDocumentId = str; //Content Dcoument ID | |
cdl.ShareType = 'V'; | |
cdl.Visibility = 'InternalUsers'; | |
cdl_List.add(cdl); | |
} | |
} | |
system.debug('Size : '+ cdl_List); | |
if(cdl_List.size()>0) | |
ContentDocumentLink_TriggerHelper.shareFile(cdl_List);// Insert Content Document Link to share the file with Account | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you'd possibly set '==' instead of '=' on line 31 😅