function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AndyPandyAndyPandy 

Render Custom Object as PDF (via VF Page)

Hi All,

 

I'm very new to VF Pages and coding in general - but am keen to learn, and have researched this query a lot, but as yet I haven't found an explanation I can understand and follow.

 

Basically, I have created a custom object (EDIT: called "Adverse Incident") and I am attempting to (end goal) have a button on the record page of the new object which will render that record as a pdf.

 

As I understand it, this must be done via a VF Page - and I am already stuck.  Reading around I have managed to create the following start (very early stages):

 

<apex:page renderAs="pdf" Controller="Adverse_Incident">
<apex:panelGrid columns="2" width="100%">
<apex:outputText value="{ !Account__r.Name }"> </apex:outputText>
<apex:outputText value="{ !Product_Code__c }"> </apex:outputText>
<apex:outputText value="{ !Batch_Number__c }"> </apex:outputText>
<apex:outputText value="{ !Surgeon__r.Name }"> </apex:outputText>
</apex:panelgrid>
</apex:page>

 Doing this, Salesforce automatically created the following Class for me:

 

public with sharing class Adverse_Incident {
}

 However, when I access the new page, it just displays the actual text "!Account__r.Name", "!Product_Code__c" etc... (not the content of the fields, as inputted by users).

 

Can someone point me in the right direction and explain where I'm going wrong please - so I can start to better understand how it works - which should enable me to progress on the VF Page, and start to introduce proper formatting etc.

 

As much step-by-step information as possible would be greatly appreciated - and please let me know if you need more information from me.

 

Many thanks,

 

Andy

Ispita_NavatarIspita_Navatar

 

Hi,

Please remove the extra space from merge fields.

Do use it in the following manner:

<apex:page renderAs="pdf" Controller="Adverse_Incident" standardStylesheets="false">

      <apex:panelGrid columns="2" width="100%">

      <apex:outputText value="{!a.Name}"></apex:outputText>

      </apex:panelgrid>

</apex:page>

On controller use your query:

public class Adverse_Incident {

public  Account a{get;set;}

   public Adverse_Incident ()

   {

        a=new account();

       a=[select name from account limit 1];

     

   }

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

AndyPandyAndyPandy

Hi,

 

Thank you for your swift response, much appreciated.

 

I have utilised your code, and just as a starter saw that the newly rendered pdf page does indeed display the Account Name (excellent).

 

However, I attempted to mimic your coding, but this time to display some of the fields from my custom object (Adverse Incident) - but there was an error:

 

Your Code (which works):

<apex:page renderAs="pdf" Controller="Adverse_Incident" standardStylesheets="false">

      <apex:panelGrid columns="2" width="100%">

      <apex:outputText value="{!a.Name}"></apex:outputText>

      </apex:panelgrid>

</apex:page>

 My Code (which does not work):

<apex:page renderAs="pdf" Controller="Adverse_Incident" standardStylesheets="false">

      <apex:panelGrid columns="2" width="100%">

      <apex:outputText value="{!a.Name}"></apex:outputText>
      <apex:outputText value="{!a.Batch_Number__C}"></apex:outputText>
      <apex:outputText value="{!a.Product_Code__C}"></apex:outputText>

      </apex:panelgrid>

</apex:page>

 It receives the error message: Error: Invalid field Batch_Number__C for SObject Account

 

So then I change the line:

<apex:outputText value="{!a.Batch_Number__C}"></apex:outputText>

 to:

<apex:outputText value="{!Batch_Number__C}"></apex:outputText>

 and get the error message: Error: Unknown property 'Adverse_Incident.Batch_Number__C'

 

Any ideas?  Can you teach me how to reference fields from either Adverse Incident object or Account object?

 

Thanks,

 

Andy

GangulyGanguly

Hi Andy,

 

Batch_Number__c will not be existing in your Account object. So you got that error "Invalid field Batch_Number__C for SObject Account". 

Make sure that the field exists and you query it in your soql in the controller as well, and you wont receive this error.

In your VF page, your initial line was correct. i.e:

<apex:outputText  value="{!a.Batch_Number__c}"></apex:outputText>

 

Please mark it solved, if it helped you in achieving your desired result.

 

Thanks,

Kaushik

AndyPandyAndyPandy

Hi Ganguly,

 

Batch_Number__c does not exist in the Account object, you're right, however, it does exist in my new Object "Adverse Incident" - and that is what I am trying to display on my VF Page, fields from my new Object "Adverse Incident".

 

Please can you help to explain how I can display fields from "Adverse Incident" on my VF Page?  Once I know that I can then mark this as solved.

 

Many thanks,

 

Andy

GangulyGanguly

Hi Andy,

 

Please post your Controller code as well as your VF page code which you are using currently for this task. Also say about your full requirement. I will see it and guide you in the correct direction.

 

Thanks,

Kaushik

AndyPandyAndyPandy

Thanks Ganguly,

 

APEX Class is:

public class Adverse_Incident {

public  Account a{get;set;}

   public Adverse_Incident ()

   {

        a=new account();

       a=[select name from account limit 1]; 

   }

}

VF Page so far is:

<apex:page renderAs="pdf" Controller="Adverse_Incident" standardStylesheets="false">

      <apex:panelGrid columns="2" width="100%">

      <apex:outputText value="{!a.Name}"></apex:outputText>

      </apex:panelgrid>

</apex:page>

 

My end goal is to have a button on the record page for each adverse incident, which, when clicked, will turn that record into a pdf, displaying all the fields in a nicely laid out pdf.

 

At this stage, I am just learning how to create the VF Page (rendered as a pdf) to include the fields from an Adverse Incident record (I shall then look into formatting it nicely).

 

I hope this makes sense - please let me know if you need more information - thank you for your help with this,

 

Andy

GangulyGanguly

HiAndy,

 

In your SOQL you are querying Account Object in which Batch_number__c field does not exist. This field exists in Adverse Incident Object. So you need to query Adverse Incident object. Eg:

Apex Code:

 

public class Adverse_Incident {

public Adverse_Incident__c aIn{get;set;}

public Adverse_Incident ()
   {
       aIn=new Adverse_Incident__c();

       aIn=[select Batch_number__c from Adverse_Incident limit 1];

   }

}

VF Code:

 

<apex:page renderAs="pdf" Controller="Adverse_Incident" standardStylesheets="false">

      <apex:panelGrid columns="2" width="100%">

      <apex:outputText value="{!aIn.Batch_number__c}"></apex:outputText>

      </apex:panelgrid>

</apex:page>

 

Try this and let me know if it does not work.

 

Thanks,

Kaushik

AndyPandyAndyPandy

Thanks again Ganguly,

 

That's a great help.

 

Just one last thing - as I want to show every single field from the Adverse Incident object on my pdf VF Page - would I have to "select" every single field individually (as per your example "aIn=[select Batch_number__c from Adverse_Incident limit 1];") - or is there a way to select them all using one line of code, then add them in to the VF Page after?

 

Thanks,

 

Andy