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
adrisseladrissel 

apex:commandLink not working per documentation - HELP!!

I have the following VF code:
<apex:page controller="Five9ScreenPop">
    <head></head>
    <body>
        <apex:form>
            <apex:commandLink name="link" action="{!NewCase}" />
        </apex:form>
    </body>
</apex:page>
Controller:
public with sharing class Five9ScreenPop{

    public PageReference NewCase(){     
               
        PageReference pr = new PageReference('http://www.google.com');        
        pr.setRedirect(true);        
        return pr;
    }
}
The SF documentation on <apex:commandLink> says "A link that executes an action defined by a controller, and then either refreshes the current page, or navigates to a different page based on the PageReference variable that is returned by the action." (http://www.salesforce.com/docs/developer/pages/Content/pages_compref_commandLink.htm)

When I load my VF page nothing happens.  No redirect, no nothing. 

What am I doing wrong here?

Thanks!!
 
Best Answer chosen by adrissel
HawkedHawked
adrissel, 

The link does not work on page load, it only works when the user click on the link. The documentation says that "the link will execute an action" and does not say anything about the link working on page load. Having said that if you are looking to call the conroller's NewCase() method on pageload, the action attribute on the <apex:page> will help you call the method right away 

<apex:page controller="Five9ScreenPop" action="{!NewCase}">

Note:The visualforce page will be redirected to google.com. You may have to relogin to salesforce based on ur browser setting.

All Answers

HawkedHawked
adrissel, 

The link does not work on page load, it only works when the user click on the link. The documentation says that "the link will execute an action" and does not say anything about the link working on page load. Having said that if you are looking to call the conroller's NewCase() method on pageload, the action attribute on the <apex:page> will help you call the method right away 

<apex:page controller="Five9ScreenPop" action="{!NewCase}">

Note:The visualforce page will be redirected to google.com. You may have to relogin to salesforce based on ur browser setting.
This was selected as the best answer
Yury BondarauYury Bondarau
As far as I remember salesforce commandLink can NOT redirect to external domain - it can redirect you only to other page of your Org.
In order to go to external domain you can use javascript methods.
adrisseladrissel

@Balaji:

When I do this all it does is refresh the page with the word "link" on it that is hyperlinked. 

@Yury

I changed it to an internal link and it did the same thing. 

When I click on the link it works, but I need it to redirect as soon as the VF page loads.
 

adrisseladrissel
Thanks.  Looks like I need to workout some client side scripting to make this work as desired.  Thank you for the clarification!
 
Yury BondarauYury Bondarau
In order to redirect page without clicking on link you can use action attribute of apex:page http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_page.htm

In your case
<apex:page controller="Five9ScreenPop" action='{!NewCase}'>
...
</apex:page>

I recommend to use this approach in case of some controller logic should be executed
But if you need just redirect to another page (without any server side logic) I'd recommend to redirect from client side (javascript or html) for instance
 
<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.yourdomain.com/index.html">