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
Rick MacGuiganRick MacGuigan 

how to prevent user from deleting a file attached to an object

I have a custom object using a Visualforce page with a related list for 'Files.' The File related list can contain a PDF version of the Visualforce page that is generated by the controller to memorialize content. If an executive reviews and comments on the Visualforce page and creates the PDF how can I prevent a File from being deleted by the owner of the custom object if certain fields are populated/updated by an executive reviewer ?
<apex:relatedList list="AttachedContentDocuments"  id="FilesList" />

 
Best Answer chosen by Rick MacGuigan
jigarshahjigarshah
Rick,

Are the reviews and comments from the Visualforce page stored on the same record where the other fields are populated / updated.

1. You may need an additional set of audit fields (hidden on the page layouts) which are updated by a Process Builder to track which fields changed and who changed them. If a PB does not work an Apex trigger will.

2. You can then write a before delete Apex trigger that checks for the following and use an addError() to restrict the deletion.
  • The fields that were modified
  • The user who modified the fields
Hope that helps.

Please mark the thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.

All Answers

jigarshahjigarshah
Rick,

Are the reviews and comments from the Visualforce page stored on the same record where the other fields are populated / updated.

1. You may need an additional set of audit fields (hidden on the page layouts) which are updated by a Process Builder to track which fields changed and who changed them. If a PB does not work an Apex trigger will.

2. You can then write a before delete Apex trigger that checks for the following and use an addError() to restrict the deletion.
  • The fields that were modified
  • The user who modified the fields
Hope that helps.

Please mark the thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.
This was selected as the best answer
Rick MacGuiganRick MacGuigan
Thanks for the reply jigarshah. Yes the data fields on the generated PDF exist on the same record where the 'pdf File' is stored. there are hundreds of fields so I think either the trigger approach would be simpler. 
jigarshahjigarshah
Rick, I am glad I was able to help.