0

I have a commandLink in a table that calls a method in the apex controller when clicked. I want it to open the page that has been assigned in the pageReference but instead its loading the same page and when I hover over the href is not showing the right page I have tried couple of different approach but none of them works.

the below VF code is residing on the Main.vfc and when the user clicks on the hyperlink I want the user to redirect to NewEmployee.vfc page.

<apex:commandLink value="{!e.Name}" action="{!clickOnName}">
     <apex:param name="eId"
     value="{!e.Id}"
     assignTo="{!empId}"/>
</apex:commandLink>

Controller:

public Id empId{get; set;}

public PageReference clickOnName() 
{
    system.debug('clickOnName//'  );
    //PageReference newPage = new PageReference('/' + Page.NewEmployee);//this did not work as well.
    PageReference newPage = Page.NewEmployee; 
    newPage.getParameters().put('empId', empId);  
    return newPage.setRedirect(true); 
} 

Also I do not see the debug message when I click on the hyperlink meaning that this action method never calls that

1 Answer 1

2

It seems like you should not use Apex at all here.

<apex:outputLink value="{!URLFOR($Page.NewEmployee, null, [empId=e.Id])}">
    e.Name
</apex:outputLink>

If you want to add more parameters, you can add them in between the square brackets.

[empId=e.Id,id=URLENCODE($CurrentPage.Parameters.Id)]
6
  • how can you get the url id along with the redirect if I go with the above code?
    – Nick
    Commented Jan 25, 2017 at 23:12
  • What do you mean by url id?
    – Adrian Larson
    Commented Jan 25, 2017 at 23:13
  • what I meant is that, when I'm on the main page I already have Id as a parameter in the url so when I redirect to a different page I want to pass that Id as well so in my case I will have NewEmployee?id=xxxx&empId=xxxxx
    – Nick
    Commented Jan 25, 2017 at 23:14
  • Then just add it to the parameters in between the square brackets.
    – Adrian Larson
    Commented Jan 25, 2017 at 23:15
  • Thanks Adrian 1+ just you know it should be Parameters ?
    – Nick
    Commented Jan 25, 2017 at 23:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .