Call Apex code from a Custom List Button #inSalesforce

Call Apex code from a Custom List Button #inSalesforce


Suppose we have the following page and class:


Page:
<apex:page standardController="Account" extensions="demoListAccountController"
           recordSetVar="sampleRecords">
    <apex:form >
        <apex:commandButton value="Demo" action="{!someAction}"/>
    </apex:form>
</apex:page>


Class:
public with sharing class demoListAccountController{
    private ApexPages.StandardSetController standardSetController; 
    public demoListAccountController(ApexPages.StandardSetController standardSetController){
        this.standardSetController = standardSetController;
    }
    public PageReference someAction(){
        List<Account> listRecords =
            (List<Account>) standardSetController.getRecords();
        List<Account> selectedListRecords =
            (List<Account>) standardSetController.getSelected();
        Boolean flag = standardSetController.getHasNext();
        PageReference ref = new PageReference('/lightning/o/Account/list?filterName=Recent');
        ref.setRedirect(true);
        return ref;
    }
}


Create a new custom button:

Object Manager | Account | demoListButton | Select List Button (display checkboxes selected) | Content Source - Visualforce Page and select the Page and Save

Then add the button to the List View button layout - Object Manager | Account | List View button layout | Edit and drag and drop from Available Buttons to Selected buttons and Save




Visual: https://www.youtube.com/watch?v=tvmWsyP_Eo4

Comments