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;
}
}