How to upload/share a file across multiple records?

The article explains how to automatically share a file uploaded on Salesforce's event object with parent records, using Apex. It details the roles of ContentDocument, representing the file; ContentVersion, its version; and ContentDocumentLink, the link to where it's shared.

How to upload/share a file across multiple records?

Hello there!

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…)

ContentDocumentLink_Trigger

ContentDocumentLink_TriggerHelper

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.

References:

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentversion.htm

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentdocument.htm

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentdocumentlink.htm

Arun Kumar
Arun Kumar

Arun Kumar is a Salesforce Certified Platform Developer I with over 7+ years of experience working on the Salesforce platform. He specializes in developing custom applications, integrations, and reports to help customers streamline their business processes. Arun is passionate about helping businesses leverage the power of Salesforce to achieve their goals.

Articles: 162

One comment

Leave a Reply

Discover more from SFDC Lessons

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from SFDC Lessons

Subscribe now to keep reading and get access to the full archive.

Continue reading