5

I am trying to setup triggers on ContentDocuments. The following link says that this is possible

https://developer.salesforce.com/releases/release/Spring12/contentdocument+triggers

I am able to create the trigger via the developer console and see it as active on my developer account. The problem is that it never gets triggered. I am expecting that this should get triggered when I upload a document in the Files tab of chatter. Here is the minimal trigger that I am testing with.

trigger content_doc_trigger on ContentDocument (after insert, after update) {
     system.debug('Trigger invoked for contentdocument');

}

Interestingly, when a trigger is made on ContentVersion, it works totally fine. Any ideas what might be going wrong here?

5

Edit

Your trigger definitely needs to fire on ContentVersion. I've quoted the relevant portions of the Object Reference below, having italicized two very important points under the ContentDocument section. You'll see that the Object Reference clearly states that the ContentVersion object is what should be used to both create and update ContentDocuments. As such, that's what your trigger should operate on.

From the Object Reference on Content Document:

Use this object to retrieve, query, update, and delete the latest version of a document, but not a content pack, in a library or a file in Chatter. Use the ContentVersion object to create, query, retrieve, search, edit, and update a specific version of a Salesforce CRM Content document or Chatter file.

• The query() call doesn’t return archived documents. The queryAll() call returns archived documents.

• If Chatter isn't enabled, Chatter files aren't included in query results.

To create a document, create a new version via the ContentVersion object without setting the ContentDocumentId. This automatically creates a parent document record. When adding a new version of the document, you must specify an existing ContentDocumentId which initiates the revision process for the document. When the latest version is published, the title, owner, and publish status fields are updated in the document.

• When you delete a document, all versions of that document are deleted, including ratings, comments, and tags.

Note: Content metadata, such as tags, custom fields, and content owners are tracked at the version level rather than at the document level.

• A document record is a container for multiple version records. You create a new version to add a document to the system. The new version contains the actual file data which allows the document to have multiple versions. The version stores the body of the uploaded document.

Here's the relevant portions of what was said about ContentVersion:

• Use this object to create, query, retrieve, search, edit, and update a specific version of a Salesforce CRM Content document or Chatter file. Use the ContentDocument object to retrieve, query, update, and delete the latest version of a document, but not a content pack, in a library or a file in Chatter.

• Use this object to create, query, retrieve, search, edit, and update a specific version of a Chatter file. Use the ContentDocument object to retrieve, query, update, and delete the latest version of a file in Chatter.

• If Chatter isn't enabled, Chatter files aren't included in query results.

• You can't query files that are only on records, whether they're public or not.

• Not all fields can be set for Chatter files.

• You can only update a version if it is the latest version and if it is published.

• You can't delete a version via the API.

• SOQL queries on the ContentVersion object return all versions of the document. SOSL searches on the ContentVersion object return only the most recent version of the document.

• Documents published into a personal library assume the default (General) content type.

Note: When Salesforce CRM Content is provisioned for an organization the administrator can create a General content type. Additional content types can be created to categorize documents. The administrator can rename the General content type.

0

Yes, run the trigger on ContentVersion then select the ContentDocument where Id = ContentDocumentId from ContentVersion. Make sure you include isLatest = true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.