6

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.

Page

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

</apex:page>

Controller class

public with sharing class ContactAction_CtrlExt {

    private Account account;
    private List<Contact> relatedContacts;


    // CONSTRUCTOR

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


    // ACTION METHOD

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

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

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

1 Answer 1

4

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

This this exact the Account ID:

enter image description here

7
  • 1
    what i saw was standard controller is on contact Commented 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/… Commented Nov 14, 2013 at 9:18
  • @RobertSösemann So is it a List View button?? Commented Nov 14, 2013 at 9:26
  • Yes. As the title states ;) Commented 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 :) Commented Nov 14, 2013 at 9:30

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.