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'); } }    ", "upvoteCount": 0, "url": "https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007T4VjjSAF", "datePublished": "2015-07-30T00:21:54.000Z", "author": { "@type": "Person", "name": "Mathew Andresen", "url": "https://trailblazers.salesforce.com/profileView?u=0053000000AzwdQAAR", "affiliation": { "@type": "Organization", "name": "Umpqua Dairy Products Company" } } }, "suggestedAnswer": [] } } Skip to main content
Hi,

I have a Visualforce page that creates a PDF contract of an opportunity, and I'm trying to save the PDF as an attachment on the opportunity.  I think I'm almost there, but I'm getting an error 

"DML currently not allowed 

An unexpected error has occurred. Your development organization has been notified."

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,

 
7 Antworten
  1. 30. Juli 2015, 00:21
    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');

    }

    }