0

I have a scenario to insert the content document link for sharing the contentdocument with contentworkspace (library) when the contentdocument is created.

I want to write a trigger to achieve this.

I have some questions regarding this:

  1. Which should I prefer, contentdocument or contentversion for writing trigger?
  2. What is the order of execution when a content version record is created with no contentdocumentId, like contentdocument is created first or contentversion, and after what operations will my trigger run?

2 Answers 2

0

In the process of uploading a document, Salesforce initially creates a ContentVersion. Following this, a ContentDocument record is automatically generated, associated with this initial ContentVersion. It's worth noting that ContentDocument cannot be directly inserted. To monitor and act upon a document upload, a trigger on contentdocument is a suitable choice.

Here's what happens when a fresh document is uploaded:

First, Salesforce creates a new ContentVersion. Here BeforeInsert trigger runs for ContentVersion. Then, it automatically sets up a ContentDocument record. The ContentDocumentId on the ContentVersion gets assigned with the ID of this freshly created ContentDocument. And the contentversion trigger runs its AfterInsert trigger with contentdocumentid.

For your sharing purpose you can write your trigger on Contentdocument

2
  • Previously, I had written trigger on contentdocument which is used to make a contentdocumentlink for sharing the contentdocument with library(contentworkspace) when document get uploaded. but, when i did it, i get the following error : Invalid cross reference Id. When i done the same thing on contentversion trigger (after insert), i faced no problem and my document shared with the library. What can be the reason?
    – riseAJdev
    Commented May 18, 2023 at 12:37
  • Can you please add some scenario which differentiates the use of trigger on contentversion and contentdocument.
    – riseAJdev
    Commented May 18, 2023 at 12:51
1

What is the order of execution when a content version record is created with no contentdocumentId, like contentdocument is created first or contentversion, and after what operations will my trigger run?

ContentVersion always creates ContentDocument for itself. You can check it with this code:

String OUTPUT_DOCUMENT_NAME = 'OUTPUT_DOCUMENT_NAME';

ContentVersion newVersion = new ContentVersion(Title = OUTPUT_DOCUMENT_NAME, PathOnClient = OUTPUT_DOCUMENT_NAME + '.csv',
                VersionData = Blob.valueOf('123'), ContentLocation = 'S');

insert newVersion;

System.assert(String.isNotBlank([SELECT ContentDocumentId FROM ContentVersion WHERE Id = :newVersion.Id].ContentDocumentId));

Which should I prefer, contentdocument or contentversion for writing trigger?

Write the trigger on ContentDocument (after insert). ContentDocumentLink needs ContentDocumentId (not ContentVersionId).

2
  • Yes you are correct, running it on ContentDocument makes sense. Aaaahhhh what was I thinking..... Commented May 17, 2023 at 11:58
  • Previously, I had written trigger on contentdocument which is used to make a contentdocumentlink for sharing the contentdocument with library(contentworkspace) when document get uploaded. but, when i did it, i get the following error : Invalid cross reference Id. When i done the same thing on contentversion trigger (after insert), i faced no problem and my document shared with the library. What can be the reason?
    – riseAJdev
    Commented May 18, 2023 at 12:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .