When you create a new enhanced Note in the UI, it creates a ContentNote and a ContentDocument record. Does anyone know the creation order of the records? Client wants me to write a trigger for the enhanced Note, and triggers are allowed only for ContentDocument, not ContentNote.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Disregard my ill-informed comment to your OP
ContentNote
is saved with key prefix 069
which is a ContentDocument
. And, ContentDocument
is the parent of ContentVersion
(key prefix of 068
).
You can think of the ContentDocument
as the header and ContentVersion as the actual “note” — with multiple versions supported. As you edit the Note and click Save, a new ContentVersion
is created – but under the same ContentDocument
Since ContentNotes
are typically added as children to some Object (like Account
or Opportunity
), the relationship between the ContentNote (i.e. ContentDocument
) and the Account or Opportunity is handled via a ContentDocumentLink
.
So, what happens first when the Note is first saved?
ContentVersion
is created (trigger possible)ContentDocument
is created (happens automatically when the first ContentVersion is saved). Trigger is also possible although it is not clear to me if you’ll get control when this object is initially inserted via the side effect of #1ContentDocumentLink
is inserted – the link(junction) between the Account (or Opportunity or …) andContentDocument
– trigger also possible
Method 2
FYI to anyone else struggling with this as I am, this is the trigger firing order when the “New” Content Note button is clicked:
ContentVersion BEFORE_INSERT ContentDocument BEFORE_INSERT ContentDocument AFTER_INSERT ContentDocumentLink BEFORE_INSERT ContentDocumentLink AFTER_INSERT ContentDocument BEFORE_UPDATE ContentDocument AFTER_UPDATE ContentVersion AFTER_INSERT ContentDocumentLink BEFORE_INSERT ContentVersion BEFORE_UPDATE ContentDocument BEFORE_UPDATE ContentDocument AFTER_UPDATE ContentVersion AFTER_UPDATE ContentDocumentLink AFTER_INSERT
and when “Save” is clicked:
ContentVersion BEFORE_INSERT ContentDocument BEFORE_UPDATE ContentDocument AFTER_UPDATE ContentVersion AFTER_INSERT
PS
Just found this detailed post Triggers on ContentObjects.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0