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
Oksana DovbakOksana 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 - 
 
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​
Best Answer chosen by Oksana Dovbak
Ajay LAjay L
You can try this:
 
​trigger deleteAttachment on Attachment (before delete) {
    // Get the current user's profile name
    Profile prof = [select Name from Profile where Id = :UserInfo.getProfileId() ];
    set<Id> oppSet = new set<Id>();    
    // If current user is not a System Administrator, do not allow Attachments to be deleted
    System.debug(Logginglevel.ERROR+'Inside trigger Name '+prof.Name);
    if (!'System Administrator'.equalsIgnoreCase(prof.Name)) {
        System.debug(Logginglevel.ERROR+'Inside IF Name '+prof.Name);
        for (Attachment a : Trigger.old) {            
            oppSet.add(a.PARENTID);
        }  
        
        if(oppSet != null){           
            Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([SELECT Id,Name,StageName FROM Opportunity WHERE ID IN: oppSet]);
            
            for (Attachment a : Trigger.old) {
                if(oppMap.containsKey(a.ParentId) && oppMap.get(a.ParentId).StageName == 'Closed Won')
                    a.addError('You can not delete, Opportunity Stage is '+oppMap.get(a.ParentId).StageName);
            }             
        }
    }
}

 

All Answers

MagulanDuraipandianMagulanDuraipandian
Hi,
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.
Oksana DovbakOksana Dovbak
Magulan,

Would you be able to provide sample code on how to do this? 

Much appreciated!
-Oksana
Ajay LAjay L
You can try this:
 
​trigger deleteAttachment on Attachment (before delete) {
    // Get the current user's profile name
    Profile prof = [select Name from Profile where Id = :UserInfo.getProfileId() ];
    set<Id> oppSet = new set<Id>();    
    // If current user is not a System Administrator, do not allow Attachments to be deleted
    System.debug(Logginglevel.ERROR+'Inside trigger Name '+prof.Name);
    if (!'System Administrator'.equalsIgnoreCase(prof.Name)) {
        System.debug(Logginglevel.ERROR+'Inside IF Name '+prof.Name);
        for (Attachment a : Trigger.old) {            
            oppSet.add(a.PARENTID);
        }  
        
        if(oppSet != null){           
            Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([SELECT Id,Name,StageName FROM Opportunity WHERE ID IN: oppSet]);
            
            for (Attachment a : Trigger.old) {
                if(oppMap.containsKey(a.ParentId) && oppMap.get(a.ParentId).StageName == 'Closed Won')
                    a.addError('You can not delete, Opportunity Stage is '+oppMap.get(a.ParentId).StageName);
            }             
        }
    }
}

 
This was selected as the best answer
Oksana DovbakOksana Dovbak
Thank you Ajay! That worked!