0

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');    
                    }    
                }    

            }
}
2
  • 1
    What have you done so far? Where are you stuck? Please take a moment to read How to Ask.
    – David Reed
    Commented Sep 5, 2019 at 19:52
  • @DavidReed-i have added helper method.
    – Maik Floyd
    Commented Sep 5, 2019 at 20:03

1 Answer 1

0
public static void limitFileForSofonFastFlow(List<Attachment> attachmentList){

        set<id> quotid = new set<id>();
        for(Attachment at:attachmentList){
            quotid.add(at.parentId);   
        }
        List<customobject__c> qt = [select id from customobject__c where id=:quotid and Recordtype.Name='channel'];
        if(qt.size()>0 && !qt.isEmpty()){
            List<Attachment> listofAttach = [select id,Name,Parentid from Attachment where Parentid=:qt and Name like '%.pdf'];
            if(listofAttach.size()>0){
                for(Attachment at:attachmentList){
                    for(Attachment wpo:listofAttach){
                        if(wpo.parentId == at.ParentId){
                            at.addError('File cannot be uploaded as pdf file already exists'); 
                        }
                    }   
                }
            }   
        }    



    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .