0

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;
    }
}
3
  • A couple of things to try: Have you tried System.debug on the CertificateIds, CertificateIds.size(), or individual elements? And, if you have, did you confirm they match actual IDs of your object? Can you confirm what is actually getting executed, and what is getting skipped over? Commented Jan 13, 2015 at 17:55
  • can you post your vf page code as well?
    – Anshul
    Commented May 23, 2015 at 4:19
  • Please update your code in the question section with the changes
    – Vignex
    Commented Aug 25, 2015 at 6:28

1 Answer 1

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.
1
  • Yes absolutely right! I added this query: lstCertif = [select Object__c from Certificate__c where Id in : CertificateIds]; but still not working
    – LoveLace
    Commented Apr 23, 2014 at 10:43

You must log in to answer this question.

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