Call Apex code from a Custom Detail Page Button #inSalesforce

Call Apex code from a Custom Detail Page Button #inSalesforce


Suppose we have the following page and class:

Page:
<apex:page standardController="Account" extensions="demoDetailAccountClass">
    <apex:form >
        <apex:commandButton value="Sample" action="{!someAction}"/>
    </apex:form>
</apex:page>


Class:
public with sharing class demoDetailAccountClass{
    private ApexPages.StandardController standardController;
    public demoDetailAccountClass(ApexPages.StandardController standardController){
        this.standardController = standardController;
    }
    public PageReference someAction(){
        Id recordId = standardController.getId();
        Account accRecord = (Account) standardController.getRecord();
        PageReference ref = new PageReference('/lightning/o/Account/list?filterName=Recent');
        ref.setRedirect(true);
        return ref;
    }
}


Create a new custom button:

Object Manager | Account | demoDetailButton | Select Detail Page Button | Content Source - Visualforce Page and select the Page and Save

Then add this button in the custom button section and mobile and lightning section of the layout. If required, add in lightning page - action section also, as shown in the video





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

Comments