How to redirect to a new VF page with command link and outputlink?

motti10 picture motti10 · Feb 1, 2012 · Viewed 29k times · Source

I have a list of Assets

Name:  column 2:  etc

A1      C1        b1
A2      c2        b2

When I click on A1, I call action="{! assetClicked}" within to do some logic, but I cannot redirect it to another Visual Force Page If I use I can link to another VF page, but cannot do action="{! assetClicked}"

Is there a way to combine them together or some other way around?

Page Code:

<apex:form >
  <apex:commandLink action="{! assetClicked}" value="{!wn.name}" id="theCommandLink"> 
    <apex:param value="{!wn.name}" name="id" assignTo="{!selectedAsset}" ></apex:param>
    <apex:outputLink value="/{!wn.id}" id="eventlink">{!wn.name}</apex:outputLink>
  </apex:commandLink> 
</apex:form>

Answer

Matt K picture Matt K · Feb 1, 2012

You would need to use the PageReference class.

Here's a modified example from the documentation:

// selected asset property
public string selectedAsset {get;set;}

public PageReference assetClicked() 
{
    // Your code here

    PageReference redirect = new PageReference('/apex/PageName'); 

    // pass the selected asset ID to the new page
    redirect.getParameters().put('id',selectedAsset); 
    redirect.setRedirect(true); 

    return redirect;
}

Alternatively, you could use Page.PageName; instead of new PageReference('/apex/PageName'); as described here.