I have a scenario where I need to check if a file has been added to a custom object record. If a file is already present, then the user should not be able to upload more files. Else, the user should be able to upload files.
Here is what I have tried.
trigger CheckFile on ContentDocumentLink (before insert) {
List<ContentDocumentLink> cdls = ( Trigger.new == null ? Trigger.old : Trigger.new );
Set<ID> parentIds = new Set<ID>();
Set<ID> NotEligible = new Set<ID>();
for (ContentDocumentLink cdl : cdls) {
parentIds.add( cdl.LinkedEntityId );
}
List<Menu__c> menuIds = [SELECT Id FROM Menu__c WHERE Id IN :parentIds];
for (Menu__c pk : menuIds){
NotEligible.add(pk.id);
}
System.debug(NotEligible);
for (ContentDocumentLink cdls : Trigger.new) {
if(NotEligible.contains(cdls.LinkedEntityId))
cdls.addError('You cannot insert an image record.');
}
}
I have a file associated with each record of a custom object. If the image file is there for the record, I do not want to be able to add more image files to it. Else, I want to be able to add a picture.