Imagine you want to add a custom button (Apex based, no JS) to the Related Contacts list on an Account detail page. To perform the buttons action you need some information of the parent Account.


<apex:page standardController="Contact" extensions="ContactAction_CtrlExt"   
           recordSetVar="contacts" action="{!doAction}">


Controller class

public with sharing class ContactAction_CtrlExt {

    private Account account;
    private List<Contact> relatedContacts;


    public ContactAction_CtrlExt(ApexPages.StandardSetController standardSetController) {
        account = ...???...;
        relatedContacts = (List<Contact>) standardSetController.getRecords();


    public PageReference doAction() {
        doSomething(account, relatedContacts);

Is there a way to access the account in the Controller Extension code of your list button?

  • 1
    Do you have a custom visualforce page on the standard account layout or is it a custom button on the stanrad related list? Nov 14, 2013 at 9:11

1 Answer 1


Try to get the ID parameter of the page:

public ContactAction_CtrlExt(ApexPages.StandardSetController standardSetController) {
    String accId = ApexPages.currentPage().getParameters().get('id');
    account = [Select Id, Name From Account Where Id = :accId]:

    relatedContacts = (List<Contact>) standardSetController.getRecords();

Here is a simple example. I've created a list view button on the Contact object with Content Source "Visualforce Page". On that page i access the ID parameter:

<apex:page standardController="Contact" tabStyle="Account" recordSetVar="">
    Id: {!$CurrentPage.parameters.id}

This this exact the Account ID:

enter image description here

  • 1
    what i saw was standard controller is on contact Nov 14, 2013 at 9:14
  • @mastOr: As Mohit states the button is not embedded directly on the account page. I am using this technique: andyinthecloud.com/2013/07/16/… Nov 14, 2013 at 9:18
  • @RobertSösemann So is it a List View button?? Nov 14, 2013 at 9:26
  • Yes. As the title states ;) Nov 14, 2013 at 9:28
  • 2
    @RobertSösemann So try my solution then. It must work, this only looks so tricky but if you access the ID through ` ApexPages` you will get the ID of the Account :) Nov 14, 2013 at 9:30

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.