I have a commandLink in a table that calls a method in the apex controller when clicked. I want it to open the pageReference in a new tab.

  • The pageReference is meant to open up a Competition-Record page
  • I also don't want to hardcode the full URL so I won't have to change it between sandboxes and production. e.g. (https://eu1.salesforce.com/{!CompId})

The issue is that the link doesn't always redirects correctly. Sometimes it will open up the new tab with the new tab being a copy of itself without going to the new pageReference.

  • When the redirects fail, neither the debug message is logged or the exception is thrown. (So the method isn't getting called?)

VisualForce Page

<apex:pageBlockTable id="CandList" value="{!lstSelectableCompetitions}" var="comp">
    <apex:column headerValue="Name">
        <apex:commandLink value="{!comp.competition.Name}"  action=" {!doOpenCompetition}" target="_blank">
            <apex:param name="compId" value="{!comp.competition.Id}" />

Apex Controller (edited)

public PageReference doOpenCompetition(){

    String theId = Apexpages.currentPage().getParameters().get('CompId');

    System.debug('The Competition Id we want to redirect to is: ' + theId);

    if(theId == null || String.isEmpty(theId)){
        throw new CompetitionFinderControllerException('ERROR: The Competition Id is null!');

    PageReference compPage = new PageReference( '/' + theId );

    return compPage;

1 Answer 1


You can use URLFOR with the $Action Global Variable to make this sort of redirect "migration proof". Check out the list of valid actions here.

    action="{!URLFOR($Action.Competition__c.View, comp.competition.Id)}"
    target="_blank" />
  • That's exactly what I wanted and it seems to be working perfectly. Although I am still wondering why the other method sometimes failed work, is it some kind of race condition between page rerender and calling the function? (because neither the debug log or the exception happened)
    – Will.T
    Feb 5, 2015 at 16:18
  • I wonder if it might work better if you use assignTo on the param.
    – Adrian Larson
    Feb 5, 2015 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.