0

I need to convert my Custom controller into an extension so I can embed my VF page into a button on the account search layout. How would I go about doing this?

public class CustomAccountListController {
 public ApexPages.StandardSetController setCon {
    get {
        if(setCon == null) {
            setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                [Select Id, Name, AV_Description__c, BillingCity, BillingState, AV_Points__c, Growth_Indicators__c, Capital_Raised__c, NumberOfEmployees, AV_Founded__c, Status__c, Deal_Lead__c, Outreach_Email_Count__c, Most_Recent_Outreach_Date__c FROM Account WHERE Status__c = 'Lead-New' ORDER BY AV_Points__c Desc]));
        }
        return setCon;
    }
    set;
}

// Initialize setCon and return a list of records
public List<Account> getAccounts() {
    return (List<Account>) setCon.getRecords();
}

}

1 Answer 1

1

Add a constructor:

public CustomAccountListController(ApexPages.StandardSetController ctrl) {
    setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
            [Select Id, Name, AV_Description__c, BillingCity, BillingState, AV_Points__c, 
                    Growth_Indicators__c, Capital_Raised__c, NumberOfEmployees, 
                    AV_Founded__c, Status__c, Deal_Lead__c, Outreach_Email_Count__c, 
                    Most_Recent_Outreach_Date__c 
             FROM Account
             WHERE Status__c = 'Lead-New' 
             ORDER BY AV_Points__c Desc]));
}

Change your setCon to a standard get/set model:

  public ApexPages.StandardSetController setCon { get; set; }

You'll need to change the VF page accordingly:

<apex:page standardController="Account" recordSetVar="unused" extensions="CustomAccountListController">
6
  • I did this but now where does my Query go so I can specify what is returned? That originally resided in the SetCon method. public class CustomAccountListController { public CustomAccountListController(ApexPages.StandardSetController ctrl) { setCon = ctrl; } public ApexPages.StandardSetController setCon { get; set; } // Initialize setCon and return a list of records public List<Account> getAccounts() { return (List<Account>) setCon.getRecords(); } }
    – Trace
    Jul 7, 2020 at 16:20
  • @Trace Actually, you can set it in the constructor instead of using the parameter passed in to the constructor. I'll edit.
    – sfdcfox
    Jul 7, 2020 at 16:21
  • That worked thanks! One issue though is it only is allowing me to add to the search layout as a list button and not a detail page button. Maybe that is out of the realm of these questions but do you know why that would be?
    – Trace
    Jul 7, 2020 at 16:30
  • @Trace standardSetController allows for list buttons, standardController for detail buttons. If you want both, you'd have to create two different pages. I've done this with a common Visualforce component to reduce the amount of code I have to share.
    – sfdcfox
    Jul 7, 2020 at 16:37
  • Nevermind I figured it out! Thanks @sfdcfox you're by far one of the most helpful people on this platform!
    – Trace
    Jul 7, 2020 at 16:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .