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
forcewithuforcewithu 

Open external URL in new window programmatically from Visualforce custom controller

In my custom controller for a Visualforce page, I'm dynamically building a URL to an external vendor site that looks something like this in my sandbox:
 
Code:
externalURL + '—id=' + objectId + '&sessionid=' + mySessionId + '&url=https://tapp0-api.salesforce.com/services/Soap/class/my_Web_Service'

 
For this, the externalURL is something like https://thevendor.urlhere.com.  Of course, objectId is my Salesforce object and mySessionId is my Salesforce session so the vendor can connect to my webservice.
Once I build the URL, I want to open it in a new window (like target="_blank" for a hyperlink), leaving the original Visualforce page open in the Salesforce window.
 
I have the basic functionality working using a Redirect, but I've been unable to determine how to open it in a new window instead of overlaying the page in the existing one.
 
Best Answer chosen by Admin (Salesforce Developers) 
canonwcanonw
Correction.  Create a javascript method to open a new window.

Attach that method to onclick attribute with the url.

All Answers

canonwcanonw
Here's an idea.  This uses commandLink as an example.
Code:    <apex:form><apex:commandLink action="{!urlRedirection}" ... target="_blank" /> </apex:form>

Here's the controller class code snippet.
Code:
public PageReference urlRedirection() {

  // generate the url
  string url = ...;

  PageReference page = new PageReference(url);
  page.setRedirect(true);
  return page;
}
 
forcewithuforcewithu

Unfortunately, it's being called from an apex:commandButton which doesn't have a target attribute.

I thought I might be able to add it using apex:actionSupport, but it doesn't have a target attribute either....

 

canonwcanonw
Is there a technical reason you have to use a commandButton?

Does the end user really care if it's a command button or a hyperlink?  All they care is to get the right information at the right place. 

If you don't want to use form and commandLink, you can use outputLink to do the same thing.
forcewithuforcewithu
The one piece of the puzzle over which I have no control is that it MUST be a button.  That was dictated to me and was one of the few pieces not open to discussion.  :smileysad:
canonwcanonw
There is no target attribute for command button in standard HTML.  To accomplish the desire effect, you can use plain HTML with JavaScript. 
canonwcanonw
Correction.  Create a javascript method to open a new window.

Attach that method to onclick attribute with the url.

This was selected as the best answer
Danny IncompanyDanny Incompany

Hi,

 

We have built an e-commerce application on force.com platform where visitors can purchase using their credit card.

 

We are sending a HTTP POST from the force.com platform to a location provided by a local banking web service, the endpoint is registered within the platform as required. The POST is successful, and we receive the proper response but it's not redirecting as it should. The response contains the form to be displayed, it contains a whole html with a credit card information form on it.

 

Several attempts to display the html in a new page have been successful but the form fails to function. Is there a way for the platform to redirect automatically after the POST, loading the document that it's receiving as a response in a new page with the the URL of the Banking Collector?

 

We have tried the JS option with the onclick to open in a new window, I am not sure if this is some security related issue from force.com platform or what. The page opens in a new page but it does not open the correct URL.

 

Please see the attached shots, fail.png is the issue we are having, good.jpeg is how it should be displayed.

 

Any help will be greatly appreciated.

 

This is how it should show up in a new window (notice the URL)

Good Working

 

 

This is how it is failing  (notice the URL)

 

 

Fail not working