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

            }
}
  • 1
    What have you done so far? Where are you stuck? Please take a moment to read How to Ask. – David Reed Sep 5 '19 at 19:52
  • @DavidReed-i have added helper method. – Maik Floyd Sep 5 '19 at 20:03
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'); 
                        }
                    }   
                }
            }   
        }    



    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.