I have a requirement where i need to make sure only one pdf can be uploaded via attachment or salesforce files in an object record. In case user tries to upload another pdf in the same record it should throw him error saying pdf already exist. Please help me in implementation or pseudo code for this. Below i have done in my helper method. Error is not being thrown which i have added in my logic.
public static void limitFile(List<Attachment> newAttachmentList,map<id,Attachment> newAttachmap){
system.debug('value of trigger.new map () :::: '+attachmap);
set<id> quotid = new set<id>();
for(Attachment at:attachmentList){
quotid.add(at.parentId);
}
system.debug('value of set ::::: '+quotid);
List<Quotation__c> qt = [select id from Quotation__c where id=:quotid and Recordtype.Name='Channel Partner Ordering and Special Requests'];
system.debug('value of qt ::::: '+qt);
if(qt.size()>0 && !qt.isEmpty()){
for(Attachment att:[select id,Name,Parentid from Attachment where Parentid=:qt and Name like '%.wpopqt' limit 1]){
system.debug('value of attach parent id ::::: '+attachmap.get(att.Parentid).Parentid);
system.debug('value of att :::: '+att.ParentId);
if(att.Parentid==attachmap.get(att.Parentid).Parentid){
att.addError('File cannot be uploaded as it already exists');
}
}
}
}