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
Jonathan Wolff 7Jonathan Wolff 7 

Trigger on Content Version for 'before delete' not working

Hello, I build a trigger on ContentVersion that should block deletation of FILES when they have "Geschäftsbrief" in "Dokumentenklasse" - picklist. Sadly the error does not occure when I delete a file with this field :(
Please correct my code:
 
  
trigger ContentVersionTrigger on ContentVersion (before delete) {
    set<Id> contentId = new set<Id>();
     if(trigger.IsDelete){
         
        for(ContentVersion con : Trigger.old){
            contentId.add(con.Id);
        }
                List<Attachment> at=[select name,body,contentType from Attachment where parentid in: contentId];
              
 for(ContentVersion con : Trigger.old){
 if(con.Dokumentenklasse__c =='Geschäftsbrief'){
                       if(at.size()>0){
                       con.addError('You cant delete the file');
                       }
                       }
        }
    }
}
Suraj Tripathi 47Suraj Tripathi 47
Hi,

This is the limitation in salesforce You can't use before or after delete triggers with the ContentVersion object.
Check below link:-
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_ignoring_operations.htm


Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi
Jonathan Wolff 7Jonathan Wolff 7
So is there no possibility to make files undeletable how I want it? The trigger sample you gave me was not working too :(
Do you have an idea what to do now?
Jonathan Wolff 7Jonathan Wolff 7
So would it be able to put the trigger on content document instead of content version?
sakhisakhi
Below has same discussion with solution .

https://developer.salesforce.com/forums?id=9062I000000DLvCQAW

Did you try this .