Hope you are safe and doing well.
In this article we are going to see how we can upload/share a file (ContentDocument) across multiple records/users/groups using Apex.
Here is the scenario: Whenever someone upload a file on the event object the same file should automatically uploaded or rather shared with it’s parent records (Account/Opportunity, etc…)
First let’s see how actually Salesforce stores the files:
Content Document : This object represents the actual documents that has been uploaded to the library in Salesforce content/files. It gets automatically created when we create a ContentVersion (Child of ContentDocument) record.
Content Version: It represents a version of a specific file or Salesforce CRM content. The maximum number of version we can publish in 24 hrs is 200,000.
Content Document Link : This object represents link between Salesforce files and where it is shared. A Salesforce file can be shares across users,groups,records and content libraries.
We can create multiple records of ContentDocumentLink to share a file across multiple records (Opportunities,Accounts,Users,etc…)
As we discussed the scenario above I am going to demonstrate how event files can be share automatically with Account(Parent) object.
When we insert the file order of trigger execution of content documents objects will be like this:
First ContentVersion object trigger will fire, next ContentDocument object trigger will fire and last ContentDocumentLink object trigger will fire.
LinkedEntityId (parent record I’d) establish links between file and Record ( Account, Opportunity, Users, Group, etc…)
Output: Upload any file on Event object associated with any Account. After upload you can see the same file attached in the Account’s files related list.