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
jwolfjwolf 

PageReference method getContent not rendering all repeat tags

I am trying to send an email with Apex using a visualforce page as the html body of the email by using PageReference.getContent(); However, when I call getContent() it is rendering the page without the one repeat component. I know that the collection the repeat is iterating over has data because if I redirect to the same PageReference the repeat component gets rendered as expected. The apex code looks like this:
 

PageReference emailTemplate = Page.ProjectInvoiceTrackingEmailTemplate;Blob emailBody = emailTemplate.getContent();System.debug(emailBody.toString());

 the template looks like this:

 

<apex:page id="templatePage" sidebar="false" showHeader="false" controller="ProjectInvoiceEmailController"> <h1>Template header</h1> <apex:repeat value="{!actualProjectsToNotify}" var="project" rendered="true"> <apex:outputText value="{!project.projectName}" /> </apex:repeat> <h3>page end</h3></apex:page>

Does anyone have any ideas why this wouldn't be rendered when calling getContent(), but it is rendered when that same page reference is rendered in the browser? 

 

 

 
XactiumBenXactiumBen

I haven't had any trouble in my experience with getting page content and including it in an email.

 

The only difference between my implementations is I do something like:

 

PageReference emailTemplate = new PageReference(Page.ProjectInvoiceTrackingEmailTemplate.getUrl());

 

Also, you can create Visualforce email templates.  Is there any reason why you haven't gone for this approach?

jwolfjwolf
The reason that we didn't go with a visualforce template, is there is data from different objects and a fair amount of processing that needs to be done in order to get that data for use in the email. The visualforce I included is a simplified version of what is actually going in the email. The reason I'm not creating a new page reference is because I want to use the current controller with a different view (I don't want anything to be re-instantiated). I had tried the method you are suggesting for getting the page reference, but I am getting the same result. Any ideas as to why the page reference would be missing content when I call getContent() that is visible if I actually render the page reference (by returning it from an action method)?
XactiumBenXactiumBen
I think getContent() works a little differently than just returning the pagereference - I don't think it will use the current controller with getContent().  So basically you will have to set up all your data again in the pagereference - if your repeat tag relies on parameters that you pass into a page you will have to include these in the pagereference.  If you do need parameters in your page it might explain why you aren't getting any content.
Message Edited by XactiumBen on 05-22-2009 11:03 AM
Bruno RosanBruno Rosan

Hi,

I have similar issue. I have created Visualforce page for generating release notes (using renderAs='pdf' attribute of page tag) and when opened in browser it looks as expected. However, when I want to save it as attachment inside my apex controller using Page.getContentAsPDF, it doesn't include any of data usually rendered by <apex:repeat> tag (that part of page/pdf is missing). Other parts of pdf that use some of controllers data but aren't inside apex repeat tag, like {! myTicketProject.Name}, are rendered as expected.

Hope that someone has figured out why this is happening and can help me. Any kind of idea would be appreciated.

Thank you.

private void savePDFAsAttachment(PageReference p) {
		String name = 'Release Notes - ' + System.today().format();
		try {
			Attachment tmpReleaseNote = new Attachment(
					Body = p.getContentAsPDF(),
					Name = name,
					ParentId = myTicketProject.Id,
					ContentType = 'application/pdf'
				);
			insert tmpReleaseNote;
		} catch(Exception e) {
			ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
		}
	}