You need to sign in to do that
Don't have an account?
contentversion trigger not preventing delete
I started to build out a simple trigger from the to prevent a File record from being deleted from SalesForce Files | Triggers. These file records can be deleted from a related list on a visualforce page. The trigger is active but I can still delete an uploaded File . Any ideas ?
trigger CREContentDocVersion on ContentVersion (before delete) { for(ContentVersion cv : trigger.old){ cv.adderror('Document Cannot be deleted'); } }
As far as the cascated delete, you can use the ContentDocumentId on the ContentDocumentLink object to find the appropriate related Id to delete on the ContentDocument object.
If you do want to do that, then I would recommending moving it into the after conext of your delete trigger. It's best practice to do any additional DML operations after the database commission has been completed.
Apex validations (which is what you're doing here) will execute in either the before or after context. Salesforce generally recommends doing trigger validations in the after context (mainly because it will allow any DML errors, such as required fields, to surface first).
All Answers
In this documentation, it is stated that "You can't use before or after delete triggers with the ContentVersion object"
If that's the case you'll need to write the trigger on the Attachment object.
What I need to do is limit 'who' can delete these File records from the related list. Right now I can't even get the trigger to respond when I delete one of these File records.
http://developer.force.com/cookbook/recipe/trigger-pattern-for-tidy-streamlined-bulkified-triggers
Also, since my users would delete the document from the Apex related list tag I also need to redirect the user to the page in order for the refresh to show the complete Visualforce page. Or should I simply embed javascript to the trigger to inform the user they are not authorized to delete the document ?
Some testing: If I deactivate ContentDocumentLinkTrigger and keep ContentDocumentTrigger active I can deleted the link and the related list no longer shows the File record. However, If I open the document from the link on the related list and then click 'Delete" from the Document Content I am prohibited from deleting it as expected.
If I deactivate ContentDocumentLinkTrigger AND ContentDocumentTrigger then I can delete the link as above but the Document remains on the Files Tab.
So this requirement will require a deletion made to the ContentDocument if the ContentDocumentLink is deleted (by an authorized user.)
Any thoughts on the best approach to prevent deletion (i.e. use Javascript to alert the user) and how to best delete the contentdocumentlink and the contentdocument by an authorized user ?
As far as the cascated delete, you can use the ContentDocumentId on the ContentDocumentLink object to find the appropriate related Id to delete on the ContentDocument object.
If you do want to do that, then I would recommending moving it into the after conext of your delete trigger. It's best practice to do any additional DML operations after the database commission has been completed.
Apex validations (which is what you're doing here) will execute in either the before or after context. Salesforce generally recommends doing trigger validations in the after context (mainly because it will allow any DML errors, such as required fields, to surface first).