How to upload/share a file across multiple records?

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

ContentDocumentLink_Trigger

ContentDocumentLink_TriggerHelper

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.

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

Thanks

Arun Kumar

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s