I have a VF page rendered as pdf that has an action=savePDF(). This uses the getcontent() function to save the pdf to the parent record. However it is recurring and causing 5 copies of the pdf to attach.
I have tried the usual if trick to stop recursion with a static boolean = true and then setting to false once the attachment has inserted but it has not worked.
Please can someone have a glance over the controller and give me a pointer as to why this is recurring.
VF Page
<apex:page standardController="Certification_Candidate__c" extensions="AssessmentTableController" showHeader="false" applyHtmlTag="false" applyBodyTag="false" renderAs="pdf" action="{!SavePDF}">
<html>
</html>
</apex:page>
Controller
public class AssessmentTableController {
public List <Assessment__c> asstList {get;set;}
public String certnum {get;set;}
public String candid {get;set;}
public String centnum {get;set;}
public Asstcodes__c newcertnum;
public AssessmentTableController(ApexPages.StandardController controller){
Id candid = ApexPages.currentPage().getParameters().get('Id');
asstList = new List<Assessment__c>();
asstList = [select Candidate__c,codeTEXT__c,Expiry_Date__c, Description__c from Assessment__c where Expiry_Date__c > today and Candidate__r.id=:candid order by codeTEXT__c desc];
centnum = [SELECT Accredited_Centre__r.Centre_Number__c from Assessment__c WHERE Candidate__r.id=:candid limit 1].Accredited_Centre__r.Centre_Number__c;
certnum = [SELECT id, Description__c from Asstcodes__c WHERE name =: 'CertificateNumber' limit 1].Description__c;
}
public PageReference savePdf() {
Attachment attachment = new Attachment();
PageReference pdf = ApexPages.currentPage();
Blob body;
try {
body = pdf.getContent();
} catch (VisualforceException e) {
body = Blob.valueOf('Some Text');
}
attachment.Body = body;
attachment.Name = centnum + '-' + certnum + '-' + String.ValueOf(Date.Today().Year()).Right(2);
attachment.IsPrivate = false;
attachment.ParentId = ApexPages.currentPage().getParameters().get('Id');
attachment.ContentType = 'application/pdf';
insert attachment;
newcertnum = [SELECT Id, description__c FROM Asstcodes__c where name =: 'CertificateNumber' limit 1];
newcertnum.description__c = String.ValueOf(Integer.ValueOf(certnum)+1);
update newcertnum;
return null;
}
}
Thanks