You need to sign in to do that
Don't have an account?
Jonathan 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');
}
}
}
}
}
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');
}
}
}
}
}
Refer this code, where I have written to prevent deletion of 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
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
Thank you,
Jonathan
Refer this code, where I have written to prevent deletion of 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
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
I have this trigger working perfectly in a Sandbox but I cannot deploy to production without creating a test class. Is anyone able to help with that?
Thanks,