+ Start a Discussion
Mohammed AzarudeenMohammed Azarudeen 

Download Different PDF files By Attachment Count

I have Custom Object called Patient__c and 2 vfp pages rendered as PDF.

It has the Notes and Attachments.

If Notes and Attachments is not null for the record then it should download the first VFP page.

If Notes and Attachments is null for the record then it should download the second VFP page.

My full Code is below
List<Attachment> attInsert = new List<Attachment>();
        Blob body;

                Integer attachmentCount = [SELECT count() FROM Attachment where ParentId IN:ids];

                if (attachmentCount > 0 ){

                        PageReference pdf = new PageReference('/apex/PatienDetails?id='+invc.id);
                        body = pdf.getContentAsPDF(); 
                        Attachment attach = new Attachment(); 
                        attach.Body = body;
                        attach.name= invc.name +'_Invoice_'+ Datetime.Now() +'.PDF';
                        attach.IsPrivate = false;
                        attach.ParentId = invc.id;
                        attach.contentType = 'application/pdf';
                        attInsert.add(attach);  
                 }

                else if(attachmentCount <= 0){

                    PageReference pdff = new PageReference('/apex/PatientsIfAttachIsNull?id='+invc.id); 
                    body = pdff.getContentAsPDF();
                    Attachment attach = new Attachment(); 
                        attach.Body = body;
                        attach.name= invc.name +'_Invoice_'+ Datetime.Now() +'.PDF';
                        attach.IsPrivate = false;
                        attach.ParentId = invc.id;
                        attach.contentType = 'application/pdf';
                        attInsert.add(attach);            

                 }

        Insert attInsert;
Problem: On the List view If i click seperately and click on download it is downloading their corresponding VFP(attachment count is > 0 downloading firstVFP and attachment count is < 0 downloading secondVFP) as PDF But If I Click Multiple Record(records with attachments and without attachments) and tried to download it is downloading only the firstVFP(PatienDetails).

Multiple Selection
How can I download their corresponding VFP if multiple records selected.