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? – Sergej Utko Nov 14 '13 at 9:11
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 – Mohith Shrivastava Nov 14 '13 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/… – Robert Sösemann Nov 14 '13 at 9:18
  • @RobertSösemann So is it a List View button?? – Sergej Utko Nov 14 '13 at 9:26
  • Yes. As the title states ;) – Robert Sösemann Nov 14 '13 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 :) – Sergej Utko Nov 14 '13 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.