You need to sign in to do that
Don't have an account?
Oksana Dovbak
Trigger to not allow deletion of attachments on closed won opp
Hello,
I am trying to modify this existing trigger that prevents someone from deleting an attachment to only fire if the associated parent opportunity stage is Closed Won -
What do I need to add in here that would only fire this error for Closed Won Opps?
Thank you,
Oksana
I am trying to modify this existing trigger that prevents someone from deleting an attachment to only fire if the associated parent opportunity stage is Closed Won -
trigger AttachmentSaver on Attachment (after delete) { // Get the current user's profile name Profile prof = [select Name from Profile where Id = :UserInfo.getProfileId() ]; // If current user is not a System Administrator, do not allow Attachments to be deleted if (!'System Administrator'.equalsIgnoreCase(prof.Name)) { for (Attachment a : Trigger.old) { a.addError('Unable to delete attachments.'); } } }
What do I need to add in here that would only fire this error for Closed Won Opps?
Thank you,
Oksana
All Answers
You have to check if parentid is opportunity id. If it is, then add it to a set < Id > and query it from opportunity object where StageName = 'Closed Won' and put it in a map. Again iterate trigger.new and throw an error if the map contains the parent id.
Would you be able to provide sample code on how to do this?
Much appreciated!
-Oksana