Find your content:

Search form

You are here

Redirecting a pageReference according to a picklist value

 
Share

I created a class and include a webservice method that will generate the pdf and attach it to a record, but it's not working when I choose a specific value in the picklist in order to generate the wanted PDF.

Custom object: Certificate__c

Picklist field: Object__c

Any advice?

global class AddPdfToRecord{

    webservice static void addPDF(list<id> CertificateIds){
        //Instantiate a list of attachment object
        list<attachment> insertAttachment = new list<attachment>();
        List<Certificate__c> lstCertif = new List<Certificate__c>();
        pageReference pdf;
        for (Certificate__c Certif: lstCertif){
            for(Id CertifId: CertificateIds){

                //create a pageReference instance of the VF page
                if(Certif.Object__c == 'Certificate of employement')
                  {  
                  pdf = Page.VFPDF;
                  }
                else 
                   {  
                   pdf = Page.VFDS;
                   }
                //pass the Account Id parameter to the class.
                pdf.getParameters().put('id',CertifId);
                Attachment attach = new Attachment();
                Blob body;
                body = pdf.getContent();
                attach.Body = body;
                attach.Name = 'pdfName'+CertifId+'.pdf';
                attach.IsPrivate = false;
                attach.ParentId = CertifId;//This is the record to which the pdf will be attached
                insertAttachment.add(attach);
             }
         }
         //insert the list
         insert insertAttachment;
    }
}

Attribution to: LoveLace

Possible Suggestion/Solution #1

lstCertif is an empty list so the for loop below never executes.

    List<Certificate__c> lstCertif = new List<Certificate__c>();
    pageReference pdf;
    for (Certificate__c Certif: lstCertif){
        ... this code is never called.

Attribution to: BarCotter
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33447

My Block Status

My Block Content