function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Teagan Glenn 9Teagan Glenn 9 

Trigger on ContentDocument and ContentVersion not firing in unit test

We are trying to put some code coverage on our triggers, but finding the triggers are not firing.

We started with just a trigger on the ContentDocument SObject (after insert, before delete). The unit test creates a FeedItem that is linked to a custom SObject. We take that feed item, use FeedItem.RelatedRecordId (relates to a ContentVersion), the ContentVersion.ContentDocumentId (relates to ContentDocument) and then deletes that ContentDocument. When reviewing the log for this unit test, the code execution stops and no Trigger is fired on the delete DML.
 
private static testMethod void deleteContentDocument()
{
    Property__c property = TestSObjectFactory.buildAndInsertProperty(); //Builds with required data, returns the property requeried after insert)

    FeedItem feedItem = TestSObjectFactory.buildAndInsertFeedItem(property.Id, '.png'); // Builds a feed item with the specified extension and parentId, returns the feed item requeried after insert

    ContentVersion contentVersion = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id = :feedItem.RelatedRecordId]; //Gets the associated ContentVersion for the specified FeedItem

    ContentDocument contentDocument = [SELECT Id FROM ContentDocument WHERE Id = :contentVersion.ContentDocumentId]; //Gets the associated ContentDocument for this ContentVersion

   delete contentDocument;    
}

We have reviewed the debug log and even added debug statements. The test method completes but the trigger is never fired. Ideas?
pconpcon
Just for clarification, does it work as expected outside of the unit test?
Teagan Glenn 9Teagan Glenn 9
Yes, 100% via UI
Prady01Prady01
Hi, Yes Content document and Content Version is a tricky oject to work on but as per my understing if you look at the DOM of the object you will understand, So then coming to the point, Below is the link which might guide you on the test class.This is something I had worked long time back.
I have written the test class for update hopefully you can find a way to manipulate it for delete.

https://developer.salesforce.com/forums/ForumsMain?id=906F00000008lcnIAA

Hope this help!

Thanks
Prady01
AnjithKumarAnjithKumar
Hi Teagan,

Note the following for the ContentVersion object:
  • Content pack operations involving the ContentVersion object, including slides and slide autorevision, don't invoke triggers.
  • Values for the TagCsv and VersionData fields are only available in triggers if the request to create or update ContentVersion records originates from the API.
  • You can't use before or after delete triggers with the ContentVersion object.

for more information https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_ignoring_operations.htm (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_ignoring_operations.htm" target="_blank)
thepaulfoxthepaulfox
I'm working with Teagan on this.

Prady01 - We understand the objects better than we ever expected to know, but it makes no sense that the trigger fires when you delete via UI and not when you delete via Apex. Thanks for your test case, but that is on ContentVersion, which seems to work as documented, unlike ContentDocument.
AKumar - Thanks for the info but we are not trying to do any of the things you have listed.

If anyone wants to reproduce it's pretty easy. Create a before delete trigger on ContentDocument that just has a single debug line.

Then create a FeedItem with ContentData, find the ContentDocument Id and delete it. You'll notice that if you do this via execute anonymous or a test class the trigger doesn't fire.

However, if you create the FeedItem exactly the same way and then delete the ContentDocument through the UI, the trigger will fire and show up in the debug logs. 

Here's a gist with the trigger and the code:
https://gist.github.com/thepaulfox/3f525d0c5f9856d5ca50
CAD AdministratorCAD Administrator
Hi @Teagan and @thepaulfox,

Did you guys find a solution or a workaround for this?  I've run into the same issue.

Thanks,
Michael
thepaulfoxthepaulfox
We've been told this may be fixed in the Spring '16 release. Currently no available workaround.
goravsethgoravseth
@thepaulfox - is there a known issue for this?  we appear to be running into it also, where we cant cover a contentdocument before delete trigger with our test classes.
Kel Woodbury 1Kel Woodbury 1
Is this actually getting fixed in Spring '16?  We can't deploy a trigger to production because of this and running the test in our sandbox under Spring '16 still doesn't provide code coverage on it.
thepaulfoxthepaulfox
I checked on this and it has been pushed to Winter '17. Still no known workaround 
Art SmithsonArt Smithson
Looks like this has been resolved in Summer '16. I just wrote a trigger that fires before delete on ContentDocument, and the test class invoked the trigger fine to get 100% coverage.  Note that delete of a ContentDocumentLink still doesn't fire a trigger (hence my need to put the delete code into a ContentDocument trigger instead).
my blog sfdcmy blog sfdc
Hi smith,
 can i have test class code for before delete on ContentDocument ,i am struggleing to get the code coverage

thanx in advance 
Any help is appreciated!