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
Jon-Michael Murphey 2Jon-Michael Murphey 2 

prevent add and delete of files from record based on status field

i have a custom object Control_Action__c and it has a picklist status field called Control_Stage__c. when the stage on this field is equal to "Approved" any files on the Control Action record can NOT be Deleted/replaced and no NEW files can be added. 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Jon,

I think you can have a simple trigger on before delete context to check the conditions and delete only the records that do not have picklist value as Approved else you can show a simple error to notify the user.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Jon-Michael Murphey 2Jon-Michael Murphey 2
Here is my current coding, 3 triggers and a handler class:
trigger ContentVersionTrg on ContentVersion (before update) {
    
    if(Trigger.IsBefore && (Trigger.IsInsert || Trigger.IsUpdate)){
     
     Map<Id,String> MapOfStatus = new Map<Id,String>();
     Set<Id> SetOfCdl = new Set<Id>();
     Set<Id> SetOfCd = new Set<Id>();
     map<Id,Id> MapOfCdNCdl = new map<Id,Id>();
     for(ContentVersion cv :Trigger.new){
      SetOfCd.add(cv.ContentDocumentId);
     }
     system.debug('Set Of Content Doc'+SetOfCd);
     for(ContentDocumentLink cdl : [SELECT LinkedEntityId,ContentDocumentId FROM ContentDocumentLink WHERE ContentDocumentId IN:SetOfCd]){
      string cdlentityId = cdl.LinkedEntityId;
//Hard coding start 3 character of my custom object
      if (cdlentityId.substring(0,3) == 'a0h') {
      
       SetOfCdl.add(cdl.LinkedEntityId);
       MapOfCdNCdl.put(cdl.ContentDocumentId, cdl.LinkedEntityId);
      }
     }
  MapOfStatus = ContectDocLinkHandler.CheckStatus(SetOfCdl);
  for(ContentVersion cv :Trigger.new){
   Id cdlId = MapOfCdNCdl.get(cv.ContentDocumentId);
   if(MapOfStatus.get(cdlId) == 'Approved'){
    cv.addError('Sorry you cannot add/update this file once Control Action is Approved');
   }
 }
    }
}

trigger ContectDocLinkTrg on ContentDocumentLink (before delete,before update) {
 
    set<Id> SetOfCdlLinkId = new set<Id>();
    map <Id,String> MapOfStatus = new map<Id,String>();
    
 for(ContentDocumentLink cdl :Trigger.old){
  SetOfCdlLinkId.add(cdl.LinkedEntityId);
 }
 MapOfStatus = ContectDocLinkHandler.CheckStatus(SetOfCdlLinkId);
 for(ContentDocumentLink cdl :Trigger.old){
  if(MapOfStatus.get(cdl.LinkedEntityId) == 'Approved'){
   cdl.addError('Sorry you cannot delete this file once Control Action is Approved');
  }
 }
}

trigger ContentDocumentTrg on ContentDocument (before delete,before update) {
    
     Map<Id,String> MapOfStatus = new Map<Id,String>();
     Set<Id> SetOfCdl = new Set<Id>();
     Set<Id> SetOfCd = new Set<Id>();
     map<Id,Id> MapOfCdNCdl = new map<Id,Id>();
     if(Trigger.IsUpdate){
      for(ContentDocument cd :Trigger.new){
       SetOfCd.add(cd.Id);
      }
     }
     if(Trigger.Isdelete){
      for(ContentDocument cd :Trigger.old){
       SetOfCd.add(cd.Id);
      }
     }
     
     for(ContentDocumentLink cdl : [SELECT LinkedEntityId,ContentDocumentId FROM ContentDocumentLink WHERE ContentDocumentId IN:SetOfCd]){
      string cdlentityId = cdl.LinkedEntityId;
      if (cdlentityId.substring(0,3) == 'a0h') {
       SetOfCdl.add(cdl.LinkedEntityId);
       MapOfCdNCdl.put(cdl.ContentDocumentId, cdl.LinkedEntityId);
      }
     }
  MapOfStatus = ContectDocLinkHandler.CheckStatus(SetOfCdl);
  if(Trigger.IsUpdate){
   for(ContentDocument cd :Trigger.new){
    Id cdlId = MapOfCdNCdl.get(cd.Id);
    if(MapOfStatus.get(cdlId) == 'Approved'){
     cd.addError('Sorry you cannot add/update this file once Control Action is Approved.');
    }
   }
  }
  if(Trigger.Isdelete){
   for(ContentDocument cd :Trigger.old){
    Id cdlId = MapOfCdNCdl.get(cd.Id);
    if(MapOfStatus.get(cdlId) == 'Approved'){
     cd.addError('Sorry you cannot delete this file once Control Action is Approved.');
    }
   }
  }
}



public without sharing class ContectDocLinkHandler {

       
 // function used to check status of Jira Issue object as complete
    public Static Map<Id,String> CheckStatus(set<Id> SetOfCdl)
    {
     map <Id,String> MapOfJIStatus = new map<Id,String>();
     
     
     if(SetOfCdl.size() > 0){
      for(Control_Action__c js : [select Id, Control_Stage__c from Control_Action__c where Id IN: SetOfCdl]){
       MapOfJIStatus.put(js.Id, js.Control_Stage__c);
      }
     }
     return MapOfJIStatus;
    }
}