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
Lynn  M GrandeLynn M Grande 

Attaching file in VF controller leads to multiple attachments

I have a VF page rendering as a pdf and have added an action calling a method in controller to attach the pdf to the record.
When it runs, it attaches 5 times. How can i generate a pdf to allow for preview and have it attach only once to record?
If I put buttons on page to accomplish the attachment, I believe the pdf will show the buttons.

Any suggestions????

Thank you!!
Martijn SchwarzerMartijn Schwarzer
Hi Lynn,

Is it possible that you post your current code here, so we can help you better?

Thanks in advance!

Best regards,
Martijn Schwärzer
Lynn  M GrandeLynn M Grande
 I got the code from another post.... I have genericised it to protect the innocent :-)
This is the line in the VF page......

<apex:page standardController="Case" extensions="myrExtension" action="{!savePdf}" renderAs="PDF" showHeader="false">

This is the method in controller:

public void savePdf() {

    PageReference pdf = Page.XXXPDF;
    // add parent id to the parameters for standardcontroller
    pdf.getParameters().put('id',parentId);

    // 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 = 'xxx.pdf';
    attach.IsPrivate = false;
    // attach the pdf to the account
    attach.ParentId = parentId;
    insert attach;

    // send the user to the account to view results
    return;

  }