+ Start a Discussion

PageReference is confusing. Pls help



I'm trying to add a VF Page that is rendered as pdf to attach to 'Notes & Attachment' in my custom object. I have working page, trigger, and class that does this, except it does not successfully add the body. 


My VF Page:

<apex:page showHeader="false" renderAs="pdf">
<style type="text/css">

  background:url(http://www.google.com/footer.background.03.png) no-repeat fixed #192771;
  background-position: center bottom; /* same as 50% 100% */

 this is the body

 My Trigger:

trigger generateSCBAgreementPDF on Stem_Cell_Bank__c (after insert) {    
    for(Stem_Cell_Bank__c scb : Trigger.new) {

 My Class:

public with sharing class attachSCBPDF{
	public static void attachPDF(Stem_Cell_Bank__c scb) {
    	Attachment myAttach = new Attachment();
    	PageReference myPdf = Page.SCBPDF;//myPdfPage is the name of your pdf page
        Blob body;
        try {
            body = myPdf.getContentAsPdf();
        } catch (VisualforceException e) {
            body = Blob.valueOf('Some Text');

        myAttach.ParentId = SCB.Id;//Id of the object to which the page is attached
    	myAttach.name = 'DisplayName.pdf';
       	myAttach.body = body;
        insert myAttach;  


 The DisplayName.pdf is attached to Notes & Attachments, I see that 'Some Text' is caught and the body is not properly being rendered


The PageReference's getContent method does not work with triggers. But I believe I'm not using it directly through trigger, so I thought this would not apply to my situation.



I've been stuck on this all day and would really appreciate it if someone can shed some light on this


Thank u for your efforts in advance




While the getContentAsPDF isn't part of your trigger code, it is called from it and is thus still running in a trigger context, so the result will be the same. You'll need to get the content in a different context - from another Visualforce page for example.