The 'before insert' trigger on the ContentVersion object won't apply any changes to a custom field when uploading a new version of a file from the Files Tab. The value set by the before trigger will be overwritten with the previous version's value.
If there's no default value set while uploading a new file, it will be null.
- Expected result:
Custom fields is populated with a designated value.
- Actual result:
Custom field isn't populated.
Steps To Reproduce:
(1) Create a custom field on the ContentVersion object (e.g., "Custom_Data__c", Text, 255 characters)
(2) Create an Apex trigger on ContentVersion with before insert and after insert events:
===================================================
trigger ContentVersionTrigger on ContentVersion (before insert, after insert) {
if (Trigger.isBefore && Trigger.isInsert) {
for (ContentVersion cv : Trigger.new) {
cv.Custom_Data__c = Datetime.now().toString();
}
System.debug(Trigger.new[0].Custom_Data__c);
}
if (Trigger.isAfter && Trigger.isInsert) {
System.debug(Trigger.new[0].Custom_Data__c);
}
}
===================================================
(3) Navigate to the Files tab in Lightning Experience
(4) Upload a new file and verify the "Custom_Data__c" field shows the correct timestamp in the file details
(5) Upload a new version of the same file
(6) Observe that the "Custom_Data__c" field shows the timestamp from the original file upload rather than the new version upload time
Move the custom field logic entirely to an after insert trigger for new versions.
005167128

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.