function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
HarryHHHarryHH 

opening a new window from apex-code

Hello,

 

I have a visualforce page with a command Button. When this is pressed some work is done in the page-reference of the controller-extension. During this work it is sometimes necessary to open new Windows in which are PDF-Documents are generated and diplayed. Is it possible to open a second, third, forth ... window with a visualforce page rendered as PDF directly from the apex code without leaving the original window. In the pagereference I think, I can only pass one new page, which is then displayed in the actual window, but I want to stay on this window with the original content and open the others for the user, who can then print them?

 

Thanks

Harry

Best Answer chosen by Admin (Salesforce Developers) 
sravusravu

 

You need not necessarily change the visualforce page CommandButton to HTML button and then add onclick event to that button. Even the visualforce page CommandButton supports onClick event:

 

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

All Answers

MJ09MJ09

As you probably know, you can't open a window from Apex code. But you can change your VF page's commandButton to a regular HTML input type="button", and add an onClick event handler that uses JavaScript to open a new window, setting the new window's URL to the VF page you want the user to see. Does that let you do what you want to do?

HarryHHHarryHH

Hello Mr. Kahn,

 

thanks for your quick reply. I did not know that it is impossible to open a new Window from apex. So I have 2 more questions to solve my problem:

 

1) How can I change an apex command button to a html button?

2) Can I then open more than one Window and let the user remain in the old one?

 

Thanks

Harry

MJ09MJ09

You're not making the Apex code launch the new window. It's Visualforce that provides the user interface. Apex provides the behind-the-scenes logic that runs on the Salesforce servers.

 

You probably have some VF code like this:

 

 

<apex:commandButton value="Do It" action="{!SomeAction}" />

 

 

And you probably have a controller, written in Apex, that includes a method named SomeAction() that returns a PageReference to the page you want the user to see in the new window. Let's assume that the page is /apex/MyNewVFPage.

 

Change your VF code to something like this:

 

 

<input type="button" value="Do It" onClick="window.open('/apex/MyNewVFPage');" />

 

 

When the user clicks this button, the onClick event's JavaScript code will open a new window, and nagivate to the given URL.

 

There are other parameters you can use when you call window.open() -- check the JavaScript documentation for details.

 

PS. Just for the record, it's "Ms" Kahn, not Mr. :)

sravusravu

 

You need not necessarily change the visualforce page CommandButton to HTML button and then add onclick event to that button. Even the visualforce page CommandButton supports onClick event:

 

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

This was selected as the best answer
lavanya k 8lavanya k 8
Hello,
You can use  target="_blank" to redirect to new page on clicking link
<apex:commandLink style="color:blue" value="Link to Attachment" action="{!controllerMethod}" target="_blank"/>
Pankaj PPankaj P
@Lavanya : Thanks , was looking for this while using controller function to redirect in command Link.