I have a modal VF component displayed over a VF page (opened as a result of click on a button on the underlying page), from which I enter a few criteria and then click a button to generate a VF PDF page as a modal popup.
In my original code the 'window.open' redirect was in the onComplete of the button, and the PDF was created correctly. If an error was thrown, the PDF page still displayed but with an error (not my error). I do not want the PDF modal popup to be displayed at all if the flag returns false and want to show my error message thrown from the Controller code in the modal component. If there is no error, it must display the PDF.
I have read through a few posts that seem to ask the same question, and have tried to implement their solutions as below in my code, but I can't get it to work. Have I done something incorrectly or is the fact that my page is a modal component the issue here?
My VF page:
<apex:component controller="InvestmentStatementModalController">
<apex:form >
<div class="modal fade x-modal" id="statementModal" tabindex="-1" role="dialog" aria-hidden="true">
<apex:pageMessages id="ispagemsg"></apex:pageMessages>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
....
</div>
<div class="modal-body">
<div class="row">
....
</div>
</div>
<div class="modal-footer">
<apex:outputPanel id="scriptPanel" layout="none">
<script type="text/javascript">
function viewPDF() {
if ({!hasPDF}) {
window.open('{!redirectURL}','_blank','height=800,width=600,location=no,resizable=yes,toolbar=no,status=no,menubar=no,scrollbars=1', 1);
}
}
</script>
</apex:outputPanel>
<button type="button" class="x-reset" data-dismiss="modal">Close</button>
<apex:commandButton styleClass="x-submit" value="View Statement"
action="{!openInvestmentStatement}" title="View Statement"
oncomplete="viewPDF()" rerender="ispagemsg,scriptPanel" />
<br/>
</div>
</div>
</div>
</div>
</apex:form>
And the page Controller method:
public PageReference openInvestmentStatement() {
public boolean hasPDF {get;set;}
.....
string pdf = InvestmentStatementController.findIssuerPDF(issueId);
if(string.isBlank(pdf)){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'There is no PDF defined for this Issuer'));
hasPDF = false;
return null;
}
hasPDF = true;
//Use redirect to open statement in popup window
redirectURL = '../investors/InvestmentStatementPDF'+'?issueId='+issueid+'&enddate='+string.valueOf(endDate)+'&startdate='+string.valueOf(startDate)+'&investorId='+accountid;
return null;
}
And the called method in another class that gets the actual PDF page to use in redirect. If this returns nothing, then the flag in the controller is set to false:
public static string findIssuerPDF(string issueid){
Issue__c issue = [select Issuer__r.Name from Issue__c where id =: issueid limit 1];
string issuer = issue.Issuer__r.Name;
//Get the correct PDF information from metadata
list<InvestmentStatementPDF__mdt> pdfinfo = [Select PDF_Name__c from InvestmentStatementPDF__mdt
where MasterLabel =: issuer limit 1];
if(!pdfinfo.isEmpty()){
return pdfinfo[0].PDF_Name__c;
}
else{
return null;
}
}
Whether hasPDF is true or false, nothing happens - the PDF isn't displayed if the flag returns true, and the error isn't displayed if it returns false. It seems to simply rerender the modal component as if the View Statement button wasn't pressed at all. Debugging shows me in 2 tests that the flag is set to true for a true test, and false if there is no PDF to display, so I know that part works. So I know it's in the VF code something isn't right.
What have I missed or done wrong? Or how should I handle this?