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