here is my controller extension
public class savePDF {
// The extension constructor initializes the private member
// variable pageOpp by using the getRecord method from the standard
// controller.
Private Opportunity pageOpp;
public savePDF(ApexPages.StandardController stdController) {
this.pageOpp = (Opportunity)stdController.getRecord();
system.debug('id = ' + pageOpp.id);
PageReference pdf = Page.TWC_Contract;
// create the new attachment
Attachment attach = new Attachment();
// the contents of the attachment from the pdf
Blob body;
try {
// returns the output of the page as a PDF
body = pdf.getContent();
// need to pass unit test -- current bug
} catch (VisualforceException e) {
body = Blob.valueOf('Some Text');
}
attach.Body = body;
// add the user entered name
attach.Name = 'test';
attach.IsPrivate = false;
// attach the pdf to the account
attach.ParentId = pageOpp.Id;
insert attach;
}
}
I kind of hypridized the code based on the post here
http://blog.jeffdouglas.com/2010/07/14/attach-a-pdf-to-a-record-in-salesforce/Thanks,I think I got it all figured out. It seems that if you have renderAs=”PDF” on, things don’t work out right. So I have it rendering as a HTML page, and then saving as a PDF. So that is successfully working right now. When I click my button on the opportunity page it generates the contract, and then saves it as a PDF attachment on the calling Opportunity Below is what worked for me. Note the calling of the method from the action function VF page (to call the method). Note, I left out the rest of the contract code
<apex:page standardController="Opportunity" showHeader="false" extensions="savePDF" >
<apex:form >
<apex:actionFunction name="saveAttachement" action="{!saveAttachement}" rerender="allPanel"/>
<script>
window.onload=function()
{
saveAttachement();
};
</script>
</apex:form>
</apex:page>
And the Controller
public class savePDF {
// The extension constructor initializes the private member
// variable pageOpp by using the getRecord method from the standard
// controller.
Date myDate = system.today();
Private Opportunity pageOpp;
public Boolean initialised{get; set;}
//Constructor
public savePDF(ApexPages.StandardController stdController) {
this.pageOpp = (Opportunity)stdController.getRecord();
id PageOppId = ApexPages.currentPAge().getParameters().get('id');
initialised=false;
system.debug('id = ' + pageOpp.id + ' id2' +PageOppId);
}
//method to insert attachement call it on page on click of button
public void saveAttachement() {
if (!initialised && pageOpp.ID !=NULL) {
PageReference pdf = Page.TWC_Contract;
// create the new attachment
Attachment attach = new Attachment();
// the contents of the attachment from the pdf
Blob body;
try {
// returns the output of the page as a PDF
body = pdf.getContentAsPDF();
system.debug('body should be fine');
// need to pass unit test -- current bug
} catch (VisualforceException e) {
system.debug('in the catch block');
body = Blob.valueOf('Some Text');
}
attach.Body = body;
// add the user entered name
attach.Name = opportunity.account.name + ' ' + myDate + '.pdf';
attach.IsPrivate = false;
// attach the pdf to the account
attach.ParentId = pageOpp.Id;
insert attach;
system.debug(attach);
initialised=true;
} else system.debug('tried to run twice');
}
}