0

I want to be able to save a PDF visualforce page into attachments (in a custom object) I created the PDF as an Iframe in another visualforce page (see below parent page, iframe and controller)

When trying to save, I'm getting the following error message:

Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Parent, File Name]: [Parent, File Name] Error is in expression '{!savePdf}' in page pdfgeneratortemplate

Can you point out how can I fix this?

Parent

   <apex:pageBlock title="PDF Input">  

      <apex:pageBlockButtons >
        <apex:commandButton action="{!savePdf}" value="Save PDF"/>
      </apex:pageBlockButtons>
      <apex:pageMessages />

 <iframe height="600px" id="Page" name="InvoicePDF" src="/apex/InvoicePDF" width="100%"></iframe>

    </apex:pageBlock> 
  </apex:form>        

</apex:page>

Iframe

<apex:page standardController="Monthly_Cloud_Bill__c" showHeader="false" renderAs="pdf">
    <body>
      <!--  <apex:stylesheet value="{!URLFOR($Resource.pdfresources, 'styles.css')}"/> -->
        <apex:image value="{!URLFOR($Resource.Logo,'Logo.jpg')}"/> 
        <apex:panelGrid columns="1" styleClass="companyTable" width="100%">
            <apex:outputText value="{!$Organization.Name}" styleClass="companyName"/>
            <apex:outputText value="{!$Organization.Street}"/>
            <apex:outputText value="{!$Organization.City}, {!$Organization.State} {!$Organization.PostalCode}"/>
            <apex:outputText value="{!$Organization.Phone}"/>
        </apex:panelGrid>
        <apex:outputPanel layout="block" styleClass="line"/>
        <apex:panelGrid columns="1" styleClass="centered" width="100%">
            <apex:panelGrid columns="2" width="100%" cellpadding="0" cellspacing="0" columnClasses="left,right">
                <apex:outputText value="Invoice# {!Monthly_Cloud_Bill__c.name}"  styleClass="customerName"/>
                <apex:outputField value="{!Monthly_Cloud_Bill__c.lastmodifieddate}" style="text-align:right"/>
            </apex:panelGrid>
            <apex:outputText value="{!Monthly_Cloud_Bill__c.account__r.name}" styleClass="customerName"/>
          <apex:outputText value="{!Monthly_Cloud_Bill__c.contact__r.name}" styleClass="contactName"/>  
        </apex:panelGrid>
        <apex:panelGrid columns="1">
            <apex:outputText value="{!Monthly_Cloud_Bill__c.account__r.name}"/>
            <apex:outputText value="{!Monthly_Cloud_Bill__c.contact__r.mailingStreet}"/>
            <apex:panelGroup >
                <apex:outputText value="{!Monthly_Cloud_Bill__c.contact__r.mailingCity}"/>
                <apex:outputText value=", {!Monthly_Cloud_Bill__c.contact__r.mailingState}"/>
                <apex:outputText value=" {!Monthly_Cloud_Bill__c.contact__r.mailingPostalCode}"/>
            </apex:panelGroup>
            <apex:outputText value="Phone: {!Monthly_Cloud_Bill__c.contact__r.phone}"/>
        </apex:panelGrid>
        <apex:outputPanel layout="block" styleClass="lineSmall"/>

    </body>
</apex:page>

Controller:

public with sharing class PdfGeneratorController {

  public ID parentId {get;set;}
  public String pdfName {get;set;}

  public PageReference savePdf() {

    PageReference pdf = Page.PdfGeneratorTemplate;
    // add parent id to the parameters for standardcontroller
    pdf.getParameters().put('id',parentId);

    // create the new attachment
    Attachment attach = new Attachment();

    // the contents of the attachment from the pdf
    Blob body;

    try {

        // returns the output of the page as a PDF
        body = pdf.getContent();

    // need to pass unit test -- current bug    
    } catch (VisualforceException e) {
        body = Blob.valueOf('Some Text');
    }

    attach.Body = body;
    // add the user entered name
    attach.Name = pdfName;
    attach.IsPrivate = false;
    // attach the pdf to the account
    attach.ParentId = parentId;
    insert attach;

    // send the user to the account to view results
    return new PageReference('/'+parentId);

  }

}
  • How are you setting the value in the parentId variable in the class? – Sam May 23 '13 at 15:08
3

It doesn't look like you are setting the parentId value in the PDFGeneratorController - should this be picked up from the URL? At the moment this will be null, causing one of the errors that you are seeing. If it is available on the URL, you should be able to extract it in your controller via something like:

   String id = ApexPages.currentPage().getParameters().get('id');

the second error is much the same - you have a property for pdfName but I can't see that being populated anywhere.

  • Thanks Bob, I was able to save this to the Attachments section. My only problem now is that I want to save only the PDF within the Iframe, but currently it's saving the entire page (as the "save" button is located on the parent page) – Itay B May 24 '13 at 13:12
  • You aren't really saving that page - you are generating a new version of it via the controller. if you change this to: PageReference pdf = Page.InvoicePDF; you will get the iframe. You may need to pass it a different id though - I can't really tell from the code. – Bob Buzzard May 24 '13 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.