To call an Apex class method from a custom button or link on an object's detail page in Salesforce, you create a Visualforce page that uses the standard controller for the object and calls the Apex method via the action attribute. When a user clicks the custom button or link, Salesforce loads the Visualforce page, which automatically triggers the Apex method before the page renders.
The action attribute on the <apex:page> component accepts an expression that references an Apex controller method (for example, action="{!doAction}"). This method is called before the page is rendered and can optionally redirect the user to another page. This approach should not be used for page initialization or Data Manipulation Language (DML) operations directly.
Case) and an Apex extension class. Set the action attribute to reference the Apex method to be called on page load.standardController="Case" — binds the page to the Case object so that the Case record ID is automatically availableextensions="EscalCase" — references the Apex class that extends the standard controlleraction="{!caseEscalation}" — calls the caseEscalation() method in the Apex extension class when the page loads<apex:inputHidden> tag is used to pass the Case OwnerId to the Apex class<apex:page standardController="Case" extensions="EscalCase" action="{!caseEscalation}">
<apex:form>
<apex:inputHidden value="{!case.OwnerId}"/>
</apex:form>
</apex:page>
EscalCase(ApexPages.StandardController controller) accepts the standard controller and retrieves the Case recordcaseEscalation() method contains the custom business logic and returns a PageReference to redirect the user back to the Case record pagesetRedirect(true) on the PageReference to ensure a proper page redirect after processingpublic class EscalCase {
//Apex properties or variables
public Id owner {get; set;}
public Id Id { get; set; }
public Case cas { get; set; }
//constructor to get the Case record
public EscalCase(ApexPages.StandardController controller) {
cas = (Case) controller.getRecord();
Id = cas.Id;
System.debug('The case record: ' + cas);
owner = cas.OwnerId;
}
//Method that can is called from the Visual Force page action attribute
public PageReference caseEscalation() {
System.debug('Case Owner: ' + owner);
System.debug('Case Id: ' + Id);
//build your code logic here
PageReference pageRef = new PageReference('/'+Id);
pageRef.setRedirect(true);
return pageRef; //Returns to the case page
}
}
000385214

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.