I have a visualforce page which shows list of a custom object records.One column shows the Account name which has lookup relationship with the custom object.I need to add one button in each row and on click this will show the contact records for that account only in a different vf page.I have tried passing the account id in the vf page that I have created for contact list.But it gets redirected to account record. Thanks for help!

<apex:column width="5%" styleClass="mainTableRow">
 <apex:facet name="header">/apex:facet>
  <apex:panelGrid columns="4" width="100%">
   <apex:commandButton value="Show Contacts" action="{!showContacts}">
    <apex:param name="accountId" value="{!obj.Account__r.Id}" assignTo="{!accountId}"/>


As Keith has mentioned - you should post a bit of your code, it's not too clear what you want to achieve.

Let's say your VF page that shows contacts accepts the account Id:

/apex/MyContactListPage?id=001... (some Account Id here)

In that case your button can be as simple as this (very similar to the other outputLink you already have):

<apex:dataTable width="100%" value="{!CustomObjectRec}" var="obj">
    <!-- skipped 1st column -->

    <apex:column width="5%" styleClass="mainTableRow">
        <apex:outputLink value="/apex/AccountContacts">
            <apex:param name="id" value="{!obj.Account__r.Id}"/>
            Show Contacts
        <!-- similar result if you really want commandButton -->
        <apex:commandButton value="Show 2" 
            action="{!$Page.MyContactListPage + '?id=' + obj.Account__r.Id}"
            immediate="true" />

My point is - unless I don't understand something - it doesn't really have to be a commandButton. Links are "friendly" when they mean navigation to new page, that's what your users are used to plus they can Ctrl+click them / middle-click them to open in a new tab... You can style this link as a button if you really want.

Check Why does apex:param assignTo work with apex:commandLink but not apex:commandButton? for more info, whether you'll want to make it work as commandButton or you'll just style the link a bit.

  • I have poseted my code.Please suggest your answers. May 2 '14 at 9:28
  • Command button will also work for me.I just want the functionality to work.whether a command button/output link doesnot matter. May 2 '14 at 12:38

It is not clear to me from your question at which stage you have the problem. This answer relates to how to get the Account ID back into the controller so that it can be passed to the contact list page.

A conventional pattern is to set an ID value in the controller when the button is clicked by having a button of this form in every row (assuming you have the Account ID available on your custom object):

<apex:commandButton value="Show Contacts" action="{!showContacts}">
    <apex:param name="accountId" value="{!custom.AccountId}" assignTo="{!accountId}"/>

The controller then returns the new page reference including that ID value in whatever way the new page accepts it:

public Id accountId {get; set;}

public PageReference showContacts() {
    PageReference pr = Page.MyContactListPage;
    pr.getParameters().put('accountId' accountId);
    return pr;
  • @SalesforceAspiration Edit the User that you are using to develop and check "Development Mode". You should then get a full stack trace reported in the page with specific line numbers. Or make that User a "Monitored User" so you get debug logs that again will have explicit line numbers.
    – Keith C
    Apr 30 '14 at 13:05
  • Thanks Keith for your quick response.How can I pass this account Id in MyContactListPage?I have used the below code in my controller for MyContactListPage. Id recId = ApexPages.currentPage().getParameters().get('id'); However recId is giving a null value. Apr 30 '14 at 14:03
  • @SalesforceAspiration If the page is expecting the parameter to be called 'id' then its just pr.getParameters().put('id' accountId);
    – Keith C
    Apr 30 '14 at 14:25
  • Even if I have put the account id,Account id is not coming in the url.Only it is getting redirected to MyContactListPage page. Apr 30 '14 at 14:35
  • 1
    Keith, cool answer but from what I remember commandButton sucks at passing parameters: salesforce.stackexchange.com/questions/4937/…. The rerender trick might work but I believe the whole involving of Apex is bit of overkill here. I'm lazy and if I can achieve it with pure HTML / pure VF - I'll go that way. Less unit tests to write etc.
    – eyescream
    Apr 30 '14 at 22:09

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.