0

I call below vf page with a custom button on my custom invoice object. and upon pushing the button, it calls below vf page then save the invoice as pdf attachment in "notes & attachment section" however I need to refresh page after pushing the button in order to see the attachment. Is there any way to refresh the page by changing below code?

<apex:page standardController="Invoice1__c" action="{!saveAttachement}" 
           extensions="B2B_B2C_Invoice_Extensions"
           showHeader="false"> 
    <!-- contentType="application/pdf#Invoice_{!Invoice1__c.Name}.pdf" 
    <apex:include pageName="B2B_Invoices"/>-->
</apex:page>

Apex Class:

public class B2B_B2C_Invoice_Extensions {
public ID parentId {get;set;}
public String pdfName {get;set;}
public String pageCaller {get;set;}
public Boolean InvoiceUpdate {get;set;}
public PageReference pdfPage;

    public B2B_B2C_Invoice_Extensions (ApexPages.StandardController controller) {

        if(controller != null) {
            parentId = controller.getRecord().id;
            Invoice1__c inv = [SELECT id, name FROM Invoice1__c WHERE Id = :parentId];
            pdfName = 'Invoice_' + inv.Name;
        }
        getInvoiceSetting();
    }

    public void getInvoiceSetting() {
        try{
            InvoiceSetting__c invoiceSetting = [SELECT id, InvoiceUpdate__c FROM InvoiceSetting__c LIMIT 1];
            InvoiceUpdate = invoiceSetting.InvoiceUpdate__c;
        } catch (DmlException e) {
            System.debug(e);
        }
    }

    public PageReference saveAttachement() {
        pageCaller = getVFPageName();

        if(pageCaller == 'B2B_Invoices_Dynamic') {
            pdfPage = Page.B2B_Invoices;
            pdfName = pdfName;
        }
        /*else if(pageCaller == 'B2C_Invoices_Dynamic') {
            pdfPage = Page.B2C_Invoices;
            pdfName = 'B2C' + pdfName;
        }*/

        invoiceToPdfGenerate(pdfPage, pdfName, parentId);

        // send the user to the account to view results
        return new PageReference('javascript:window.close()');
        //return new PageReference('javascript:window.close()');
    }

    public void invoiceToPdfGenerate(PageReference pdfPage, String pdfName, Id parentId) {

        // add parent id to the parameters for standardcontroller
        pdfPage.getParameters().put('id', parentId);

        // the contents of the attachment from the pdf
        Blob body;

        try {
            // returns the output of the page as a PDF
            body = pdfPage.getcontentAsPdf();
            // need to pass unit test -- current bug    
        } catch (VisualforceException e) {
            body = Blob.valueOf('Some Text');
        }

        List<ContentDocument > cFileList = [SELECT id FROM ContentDocument WHERE Title =: pdfName];

        if(cFileList.size() == 0 || InvoiceUpdate) {

            ContentVersion cv = new ContentVersion();
            cv.versionData = body;
            cv.title = pdfName;
            cv.pathOnClient = pdfName+'.pdf';
            insert cv;
        }

        List<Attachment> cAttachmentList = 
            [
                SELECT id 
                FROM Attachment 
                WHERE ParentId = :parentId 
                AND Name = :pdfName+'.pdf'
            ];

        if(cAttachmentList.size() == 0 || InvoiceUpdate) {

            // create the new attachment
            Attachment attach = new Attachment();
            attach.Body = body;
            // add the user entered name
            attach.Name = pdfName+'.pdf';
            attach.IsPrivate = false;
            attach.ContentType = 'application/pdf';
            // attach the pdf to the account
            attach.ParentId = parentId;
            insert attach;
        }

    }

    public string getVFPageName() {
        String pageName = ApexPages.CurrentPage().getUrl();
        pageName = pageName.replaceFirst('/apex/','');
        pageName = EncodingUtil.urlEncode(pageName, 'UTF-8');
        string[] pageNameExtra = pageName.split('%3F',0);
        pageName = pageNameExtra[0];
        return pageName;                                        
    }
}
5
  • 1
    This is all the code for the VF page? Please Post code for extension also.
    – javanoob
    Dec 25, 2016 at 14:38
  • Please code the custom code here Dec 25, 2016 at 15:00
  • I put it on my original post
    – Irem Guner
    Dec 25, 2016 at 16:50
  • is this an inline vf page?
    – Eric
    Dec 25, 2016 at 17:24
  • no not inline @Eric
    – Irem Guner
    Dec 25, 2016 at 19:19

2 Answers 2

0

If you want to view the page after attaching it, you need to change your code under the public PageReference saveAttachement() { where you have the following:

// send the user to the account to view results
return new PageReference('javascript:window.close()');
//return new PageReference('javascript:window.close()');

Instead of just closing the window you used, you need to return the PageReference for the PDF which appears from your code to be the contents of the variable pdfPage.

4
  • Hi @crmprogdev , thank you. I don't want to see the PDF page created as a PDF, Extension code insert a PDF attachment on Invoice Custom Object Page (in Notes & Attachments Section). But once I make it, I cant see the inserted attachment line on "Notes & Attachment' section. I need to refresh the SFDC page to see it. Is there any way to make it on extension code?
    – Irem Guner
    Dec 26, 2016 at 14:39
  • You want to get the referrer page when you initially open your extension, save that to a variable, then use that as the pagereference return at the end of your last method.
    – crmprogdev
    Dec 26, 2016 at 17:46
  • yes, exactly...
    – Irem Guner
    Dec 27, 2016 at 1:22
  • In parentId you have the recordId from which to reinitialize or return to the source page from which the controller was called. It appears that your page caller is B2B_Invoices_Dynamic. All you need to do is set the Id param and return to that Page.
    – crmprogdev
    Dec 27, 2016 at 15:04
0

Try this return new PageReference(getVFPageName());

1
  • Hi @Prolay, thank you. I don't want to see the PDF page created as a PDF, Extension code insert a PDF attachment on Invoice Custom Object Page (in Notes & Attachments Section). But once I make it, I cant see the inserted attachment line on "Notes & Attachment' section. I need to refresh the SFDC page to see it. Is there any way to make it on extension code?
    – Irem Guner
    Dec 26, 2016 at 5:02

You must log in to answer this question.

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