You need to sign in to do that
Don't have an account?
Teagan 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.
We have reviewed the debug log and even added debug statements. The test method completes but the trigger is never fired. Ideas?
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?
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
Note the following for 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)
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
Did you guys find a solution or a workaround for this? I've run into the same issue.
Thanks,
Michael
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!