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
daniel1110daniel1110 

PageReference is confusing. Pls help

Hi,

 

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">
<head>
    
<style type="text/css">

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

</style>
</head>
<body>
 this is the body
</body>
</apex:page>

 My Trigger:

trigger generateSCBAgreementPDF on Stem_Cell_Bank__c (after insert) {    
    for(Stem_Cell_Bank__c scb : Trigger.new) {
        attachSCBPDF.attachPDF(trigger.new[0]);
    }
}

 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.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_pages_pagereference.htm

 

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

 

Daniel

bob_buzzardbob_buzzard

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.