I've created a visualforce page on the Opportunity object that I'm rendering as a PDF. I just started using docusign and would rather not have to generate the doc, attach it to the record and then send via DS. I want to click a button and then create the document and have it attach automatically to the source record.

How would I do this since I don't have a class that I can modify? I'm assuming I'll have to use an extension but am not clear on how I get the document to automatically attach when I click my custom button?

Any help is appreciated!

1 Answer 1


Take a look at this example code - this helped me to get PDF attachments working. https://developer.salesforce.com/page/Visualforce_Quote2PDF

Realized probably better practice to include some code snippets here. Here is a sample (very simple ) controller extension written for the object that has a PDF page - note this is not full production ready as there is no error handling included. This is based on the training exercises for VF online class.

I am not yet expert VF or Apex developer so there may be other/better ways to do this but it did work for me.

public class offerPDFExtension {
    ApexPages.StandardController controller;
    public Offer__c offer {get;set;}
    public PageReference rtn;
    public offerPDFExtension(ApexPages.StandardController c){
        offer = (Offer__c)c.getRecord();
        rtn = c.view();
    public PageReference attachOfferPDF() {
        /* Get the page definition */
        PageReference pdfPage = Page.offerPDF;
        /* generate the pdf blob */
        Blob pdfBlob = pdfPage.getContent();
        /* create the attachment against the offer */
        Attachment a = new Attachment(parentId = offer.id, name=offer.name + '.pdf', body = pdfBlob);
        /* insert the attachment */
        insert a;
        /* send the user back to the offer detail page */
        return rtn;


Here is the VF page that is called from a custom button to generate and attach the PDF to the record.

<apex:page standardController="Offer__c" extensions="offerPDFExtension" action="{!attachOfferPDF}">
<!--  Need to include field on page so the controller ext can access it-->

You must log in to answer this question.

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