Find your content:

Search form

You are here

how to get the id of a record in a row?

 
Share

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}"/>
   </apex:commandButton>
  </apex:panelGrid>
 </apex:column>
</apex:dataTable>


Attribution to: SalesforceAspiration

Possible Suggestion/Solution #1

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
        </apex:outputLink>
        <!-- similar result if you really want commandButton -->
        <apex:commandButton value="Show 2" 
            action="{!$Page.MyContactListPage + '?id=' + obj.Account__r.Id}"
            immediate="true" />
    </apex:column>
</apex:dataTable>

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.


Attribution to: eyescream

Possible Suggestion/Solution #2

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}"/>
</apex:commandButton>

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;
}

Attribution to: Keith C
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/34071

My Block Status

My Block Content