+ Start a Discussion

How to get id for record on commandlink press ?

Hi All,

I am get getting list of accounts as command links in visual force page once i click a particualr account releated contact details record names are 

displayed as command links again if i click on particular contact i need to redirect  contact record detail page instead of it i am redirecting to home

page i guess i am getting home page ID instead of Contact ID  and help me how to get record ID on CommandLink Click 

<apex:page controller="MyController">
    <apex:form >
     <apex:pageBlock title="Accounts Info">
     <apex:pageBlockSection title="Accouns">
     <apex:dataList value="{!myaccounts}" var="acct">
       <apex:commandlink action="{! accountClicked}" rerender="ContactDetail">
    <apex:outputText value="{! acct.name}"/>
    <apex:param name="id" value="{! acct.Id}" assignTo="{!selectedAccount}"/>
    <apex:pageBlockSection title="Contact Records">
    <apex:outputPanel id="ContactDetail">
    <apex:repeat value="{! contactsInformation}" var="contact">
     <p><apex:commandLink action="{!contactClicked}">
      <apex:outputText value="{! contact.Name}"/>
     <apex:param name="id1" value="{!contact.id}" assignTo="{!selectedContact}"/>

---contrlooer code---

public class MyController {

    public Id selectedAccount { get; set; } 
    public List<Contact> contactsInformation { get; set; }
    public Id selectedContact { get; set; } 
    public PageReference contactClicked() {
    List<Contact> id = [select id from contact where id =:selectedContact];
      PageReference nextpage = new PageReference('https://na15.salesforce.com/?id='+id);
                return nextpage;


    public List<Account> getMyaccounts() {
        return [SELECT Id, Name, AccountNumber FROM Account ORDER BY
           LastModifiedDate DESC LIMIT 10];

    public void accountClicked() {
    contactsInformation = [SELECT Name,id FROM Contact
            WHERE AccountID = :selectedAccount];


Kindly  chnage contactClicked method to below code and dont hardcode the url  -:

public PageReference contactClicked() {
         return new PageReference('https://na15.salesforce.com/?id='+selectedContact);

To avoid hardcoding check this link -:


If this resolves your issue, kindly MARK it as SOLVED.