+ Start a Discussion
HCrousePDXHCrousePDX 

opening a new window from apex-code after validation

I have a button on a visualforce page that will generate a report using a url in another window, similar to the code below:

 

<apex:commandButton value="Submit" action="{!controlleraction}"  onClick="window.open('/apex/VFPage');"/>

 

What I want to do is to stop the onClick event from happening if the controlleraction has an error.  Basically when they click the button I need to run some validations to make sure the report won't be blank or contain in correct data.  So I want to first run a validation routine in the controller, if it passes then I want the report to generate.  If not, I don't need the report to open; the controller method will return an error to the page.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JayNicJayNic

I'm a big fan of action functions, and rerenders I use as little apex as possible when it comes to interface logic. Here is how I would approach the situation:

 

Create a "success" boolean in your controller that can be set to true or false during yoru controller action. 

Use an action function to call the controller action, and on complete: rerender an area of the page with the resulting success boolean as a javascript variable

Use the javascript variable to determine if the user should get redirected, or not

 

Something like this:

 

<apex:actionFunction name="af_myControllerAction" action="{!myControllerAction}" reRender="scripts, msgs" oncomplete="checkSuccess()"/>

<input type="button" class="btn" value="Go!" onclick="af_myControllerAction()"/>

<apex:pageMessages is="pageMessages"/>

<apex:outputPanel id="scripts>
    <script>
        var success = {!success};
        function checkSuccess(){
            if(success) {
                window.top.location = myUrl;
            }   
        }
    </script>
</apex:outputPanel>

 

 

This way you neve rhave to use those silly pageReferences, and you can add as much logic before the button click, and after

All Answers

Suresh RaghuramSuresh Raghuram

Here you can do as follow.

 

when u do validation check using some method and if this validation didn't went through you will raise some error message.

 

so do a check on the error message == null. then call the method which will generate the report.

 

the only thing just do check like message.size() ==null or message == null which ever fit over there and then do the as usual task that you are doing now.

 

If this answers your make this as a solution and Give KUDOs. PLZ.

 

Thanks,

Suresh.

JayNicJayNic

I'm a big fan of action functions, and rerenders I use as little apex as possible when it comes to interface logic. Here is how I would approach the situation:

 

Create a "success" boolean in your controller that can be set to true or false during yoru controller action. 

Use an action function to call the controller action, and on complete: rerender an area of the page with the resulting success boolean as a javascript variable

Use the javascript variable to determine if the user should get redirected, or not

 

Something like this:

 

<apex:actionFunction name="af_myControllerAction" action="{!myControllerAction}" reRender="scripts, msgs" oncomplete="checkSuccess()"/>

<input type="button" class="btn" value="Go!" onclick="af_myControllerAction()"/>

<apex:pageMessages is="pageMessages"/>

<apex:outputPanel id="scripts>
    <script>
        var success = {!success};
        function checkSuccess(){
            if(success) {
                window.top.location = myUrl;
            }   
        }
    </script>
</apex:outputPanel>

 

 

This way you neve rhave to use those silly pageReferences, and you can add as much logic before the button click, and after

This was selected as the best answer
HCrousePDXHCrousePDX

This is what I needed, Thanks!