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 uploads a file on the event object the same file should automatically upload or rather be shared with its parent records (Account/Opportunity, etc…)
First let’s see how actually Salesforce stores the files:
Content Document: This object represents the actual documents that have 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 versions we can publish in 24 hrs is 200,000.
Content Document Link: This object represents a link between Salesforce files and where it is shared. A Salesforce file can be shared 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 in the scenario above I am going to demonstrate how event files can be shared automatically with the 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, the next ContentDocument object trigger will fire and the last ContentDocumentLink object trigger will fire.
LinkedEntityId (parent record I’d) establishes links between file and Record ( Account, Opportunity, Users, Group, etc…)
Output: Upload any file on the Event object associated with any Account. After upload, you can see the same file attached in the Account’s files related list.