+ Start a Discussion
Jonny SimmoJonny Simmo 

Can you close a Visualforce page from the apex controller?

Hello there,

Is it possible to close the current Visualforce window from the apex controller?

I have a submit button which when clicked uses the action="{!submit}" to call a Submit method in my controller, this submit method does some validation and if successful writes a record to the database, at this point I want to close the window from the controller. Is this possible?

I don't want to use the oncomplete("") option for the button to close the window as if the validation fails in the submit method I want to keep the window open.

Many thanks

 
Best Answer chosen by Jonny Simmo
Jonny SimmoJonny Simmo
Ok I have managed to resolve it.

What I did was to do it all within my Submit method in the apex controller.

Id the validation and if

Validation fails return to the page and show a message...

            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Please answer the questions before clicking submit'));
            // Return to the post chat survey page
            PageReference pageRef = ApexPages.currentPage();
            return pageRef;

If validation passed closed window...

            return new PageReference('javascript:window.close()');

Thanks for your help.
 

All Answers

Rahul Sangwan7341Rahul Sangwan7341
Hi Jonny,

Do one thing in apex class set a boolean variable to true or false and on your button onclick call the javascript function and check if the value of the variable which you set in class and based on that close the window.

Please mark this as Best Answer if it helps you.
 
Abhi_TripathiAbhi_Tripathi
Hi Johny,

If you don't want to use Oncomplete then don't use

try this
1. Create actionFunction and call you apex method that will return true or false on basis of successfully criteria completion and vice versa.
2. Create a function of javascript on page and call that actionfunction attribute name that you created.
     In the function a var will hold the response of the method.
3. On the basis of the response of the method that you called in javascript function, close the window or let it open.
4. Call the javascript function from an html button.

Hope this helps...

Regards,
Abhi Tripathi
Salesforce Developer
http://abhithetechknight.blogspot.in/
Rahul Sangwan7341Rahul Sangwan7341
yes abhi_tripathi same thing i explained..........
Jonny SimmoJonny Simmo
Ok thanks for the replies.

My Submit button calls the submit() method in my Controller class. I set a variable to true or false depending on validation.

On my Command button I call a Javascript function using onclick"".

The problem now is that when I click the Submit button it calls the onClick javascript function before running the Submit method in the Controller class, so the onclick validation takes place too early. I determined this by using Alerts and worked out that I have to click the Submit button twice as it only picks up the value in the Javascript on the second click as it gets the valus set in Submit from the first click.

Hope that makes sense.

 
Jonny SimmoJonny Simmo
Ok I have managed to resolve it.

What I did was to do it all within my Submit method in the apex controller.

Id the validation and if

Validation fails return to the page and show a message...

            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Please answer the questions before clicking submit'));
            // Return to the post chat survey page
            PageReference pageRef = ApexPages.currentPage();
            return pageRef;

If validation passed closed window...

            return new PageReference('javascript:window.close()');

Thanks for your help.
 
This was selected as the best answer