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


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;


  • 2
    Very bad practive -- having action on page load. It is being called when you call getContent() in your savePDF method. I suggest you to create another VF page to trigger this method from that by doAction (which is still not a good idea but you will not have a loop at least)
    – kurunve
    Dec 18, 2017 at 11:47
  • Ahh you're right!I have just replaced body = pdf.getContent(); with body = Blob.valueOf('Test'); and it hasn't recurred. I'm wondering if I can fetch the body of the vf page between <html> tags and then blob getcontentAsPDF()?
    – Joe Hayes
    Dec 18, 2017 at 12:04
  • 1
    No, unfortunately that is not possible. Great gift from salesforce (getContent and getContentAsPDF works with VF pages or some other url in SF). If you really dont want to add new VF page, I can suggest to add another URL param, for which savePDF should not do anything in case if that is set to true, and call getContent with that extra param
    – kurunve
    Dec 18, 2017 at 12:11
  • The extra param sounds like a very good idea! Thank you. I am still fairly new to all this, please can you give me a very quick example of this? I can add the param into the button url that calls the vf. I'm just not sure how I would write it into the controller. Thanks
    – Joe Hayes
    Dec 18, 2017 at 12:14

1 Answer 1


I ended up sorting this by adding a parameter to the button url called savecopy and then had an if statement at the beginning of my savePdf() and then used getParameters().remove(savecopy) inside the savePdf() method so that when it recurred the save copy param was no longer there and the if returned null. This seems to work fine and now only adds one attachment. Thanks for your help kurunve

You must log in to answer this question.

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