+ Start a Discussion
Jonathan Wolff 7Jonathan Wolff 7 

Replace before delete Content Version trigger

Hello, I have a before delete Trigger on Content Version. A user here told me that there is a limitation that does not allow before delete triggers to work on content version. Do you know a possibility how to overcome this problem?
The trigger code is:

trigger ContentVersionTrigger on ContentVersion (before delete) { Set<Id> contentDocumentIdSet = new Set<Id>();

if(trigger.IsDelete){ for(ContentVersion con : Trigger.old){

if(con.ContentDocumentId != null) { contentDocumentIdSet.add(con.ContentDocumentId); } } ContentDocumentLink cdl = [SELECT ContentDocumentId,

LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId IN:contentDocumentIdSet Limit 1];

for(ContentVersion con : Trigger.old){

if(con.Dokumentenklasse__c =='Geschäftsbrief'){
if(cdl!=null){

con.addError('You cant delete the file');
}
}
}
}
}
Best Answer chosen by Jonathan Wolff 7
Surya GSurya G
Hi Jonathan,

Refer this code, where I have written to prevent deletion of file of type 'PNG'.
trigger ContentDocumentTrigger on ContentDocument (before delete) {
    
	map<id,ContentDocument> documentMap = new map<id,ContentDocument>([SELECT id,(SELECT Id,FileType from ContentVersions) from ContentDocument where id IN :trigger.old]);
 
        for(ContentDocument con : Trigger.old){
           List<ContentVersion> versionList = documentMap.get(con.Id).ContentVersions;
            if (versionList.size() > 0) {
                for (contentVersion cv : versionList) {
                    if(cv.FileType == 'PNG') {
                        con.adderror('cannot delete file of type PNG');
                    }
                 }
            }
            
        }
}
Content Document is parent of content Version.

Content Document: It Represents a document that has been uploaded to a library in Salesforce CRM Content or Salesforce Files. The maximum number of documents that can be published is 30,000,000. This object record you don’t have to create. It gets created when you create ContentVersion which is the child of ContentDocument.

Content Version: Represents a specific version of a document in Salesforce CRM Content or Salesforce Files. In other words, this object stores document information similar like Attachment

please refer those support documents, that give some insight about  content version and content document.
https://developer.salesforce.com/forums/?id=9060G0000005Y9rQAE
https://salesforce.stackexchange.com/questions/160198/relationship-between-contentversion-object-and-document-object/270094

Hope it helps

Thanks 
Surya G
 

All Answers

Surya GSurya G
Hi Jonathan, 

you have to write trigger on content document. Please refer to this link, it might help you for your requirement.
​​​​​​​https://developer.salesforce.com/forums/?id=9060G0000005Y9rQAE (http://https://developer.salesforce.com/forums/?id=9060G0000005Y9rQAE)

Thanks
Surya G
Jonathan Wolff 7Jonathan Wolff 7
Hi Surya, could you give me a code sample to achive this? sadly I'm quite new in developing triggers.
Thank you,
Jonathan
Surya GSurya G
Hi Jonathan,

Refer this code, where I have written to prevent deletion of file of type 'PNG'.
trigger ContentDocumentTrigger on ContentDocument (before delete) {
    
	map<id,ContentDocument> documentMap = new map<id,ContentDocument>([SELECT id,(SELECT Id,FileType from ContentVersions) from ContentDocument where id IN :trigger.old]);
 
        for(ContentDocument con : Trigger.old){
           List<ContentVersion> versionList = documentMap.get(con.Id).ContentVersions;
            if (versionList.size() > 0) {
                for (contentVersion cv : versionList) {
                    if(cv.FileType == 'PNG') {
                        con.adderror('cannot delete file of type PNG');
                    }
                 }
            }
            
        }
}
Content Document is parent of content Version.

Content Document: It Represents a document that has been uploaded to a library in Salesforce CRM Content or Salesforce Files. The maximum number of documents that can be published is 30,000,000. This object record you don’t have to create. It gets created when you create ContentVersion which is the child of ContentDocument.

Content Version: Represents a specific version of a document in Salesforce CRM Content or Salesforce Files. In other words, this object stores document information similar like Attachment

please refer those support documents, that give some insight about  content version and content document.
https://developer.salesforce.com/forums/?id=9060G0000005Y9rQAE
https://salesforce.stackexchange.com/questions/160198/relationship-between-contentversion-object-and-document-object/270094

Hope it helps

Thanks 
Surya G
 
This was selected as the best answer
Jonathan Wolff 7Jonathan Wolff 7
Hi Surya,
I wanted to try it with your code sample but I dont know how to use it properly. I have the error that Dokumentenklasse does not exist as a field, although I have it in my Content version. Could you give me a code for my specific problem :( it is quite desperating 
Jonathan Wolff 7Jonathan Wolff 7
made it :D
Surya GSurya G
Awesome :)