I'm trying to use getContentasPDF on a custom VF page which references a VF component. This VF page is not exposed on UI to users and it's sole purpose is for generating it as PDF and saving/sending it as an attachment.

I am passing the record ID rId in the method so that only child records are retrieved for the rId in the PDF(This is done using the controller on the VF component). But some default records are getting retrieved in PDF. Even preview of VF page returns some default records.

How to return only associated records passed in the method?

VF Page

<apex:page Controller="ReportPDF" showHeader="false" sidebar="false">
  <apex:outputText style="font-style:bold" value="Blah Blah --.">
  <apex:param name="q" value="{!expreportid}"/>
  <c:ExpenseComponent expense_report="{!expreportid}"> 

Apex class

    public class ReportPDF {

        public String expreportid { get; set; }

                public static void sendPDF(Id rId, Id recipientId){
                    blob body;
                    pageReference pageref = page.ExpensePDF;

                         body = pageref.getContentAsPDF();

                    catch(calloutException ce)

                      Attachment pdfatt = new Attachment();

                      pdfatt.parentid = reportId;
                      pdfatt.body = body;
                      pdfatt.name = 'test';

                      insert pdfatt;
                  catch(DMLexception de){


VF Component Controller

 public class FindExpenseLines {

 public id reportid {get;set;}

 public List<Expense_Item__c> getExpenseItems() {
 return [select Source__c, Destination__c, Mode__c,
    Kms_Travelled__c, Expense_Incurred__c, createdby.name, createdby.EmployeeNumber from expense_item__c where Expense_Report__c =:reportid];

VF Component

<apex:component controller="FindExpenseLines" access="Global">
 <apex:attribute name="expense_report" type="Id" assignTo="{!reportId}" description="Id of the expense report"/>

 <table border = "2" cellspacing = "5">
            <td>Appointment Time</td>
            <td>Exp Item Number</td>
            <td>Exp Report</td>
            <td>Client Visited</td>
            <td>Agency Visited</td>
            <td>Lead Visited</td>
            <td>Source Location</td>
            <td>Destination Location</td>
            <td>Mode of Travel</td>
            <td>Amount of Expenditure</td>               

        <apex:repeat value="{!expenseItems}" var="item">
  • So, you have a controller for <c:ExpenseComponent/> that is supposed to query records based on expense_report? Any chance we can see that too? Also, what is calling sendPDF? Commented Feb 25, 2016 at 16:16
  • @PhilHawthorn I've added the component controller. sendPDF - I am gonna call this on a TriggerHandler (Tony's tidy pattern)
    – Jarvis
    Commented Feb 25, 2016 at 19:26

2 Answers 2


If you are passing rId as q in on the URL in your Apex, wouldn't you then want to use it in your visualforce like this?

<apex:page Controller="ReportPDF" showHeader="false" sidebar="false">
  <apex:outputText style="font-style:bold" value="Blah Blah --.">
  <c:ExpenseComponent expense_report="{!$CurrentPage.parameters.q}"> 

To test the output, you can see what appears if you manually build your URL in the browser as:

https://<pod>.salesforce.com/apex/ReportPDF?q=<some id>

If you still get rogue records, I would suggest something is amiss in the controller for <c:ExpenseComponent/>.

  • Did you omit <apex:param name="q" value="{!expreportid}"/> advertently ?
    – Jarvis
    Commented Feb 25, 2016 at 19:29
  • Yes, i wasn't sure what it was for....what is it for? Commented Feb 25, 2016 at 19:50
  • Sorry, VFnoob here! I learnt that <apex:param is used to getting & setting parameters for a VF page. So was trying to pass ID from the controller
    – Jarvis
    Commented Feb 25, 2016 at 19:54
  • I don't think you need it, you're going to pass in the parameter from the URL to your component, you can reference it as shown in the answer Commented Feb 25, 2016 at 19:56
  • 1
    So, I just mocked this up in my DE org and it works perfectly. I took your page, component and controller and used Contact (instead of expense) and it worked fine as far as inserting the attachment (i didn't test the email). You're either not describing the circumstances very well, or something else is at play. I'm out now I'm afraid :( Commented Feb 26, 2016 at 14:43

In your Controller constructor using ApexPages.CurrentPage().getparameter.get('q'); get the ID and assign to expreportid . It will solve your problem.

  • I'm passing ID in the static method's parameter. How will constructor be called in that case?
    – Jarvis
    Commented Feb 25, 2016 at 19:38
  • @Bennie ` pageReference pageref = page.ExpensePDF; pageRef.getParameters().put('q',rId); ` I am talkiing about this. Get this value in page constructor and hen use. Or get in page using as @Phil suggested. Commented Feb 26, 2016 at 5:02
  • I've followed what Phil suggested, the URL https://<pod>.salesforce.com/apex/ReportPDF?q=<some id> works fine but the pdf is still returns some rogue records.
    – Jarvis
    Commented Feb 26, 2016 at 5:21
  • check if you are getting correct Id in component . Commented Feb 26, 2016 at 6:45

You must log in to answer this question.

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