To call an Apex class from custom button or link on the object detail page, create a VisualForce page and call the Apex class method via the action attribute to make it work. Following is some sample code showing how to do that.
The action method invoked when this page is requested by the server. Use expression language to reference an action method. For example, action="{!doAction}" references the doAction() method in the controller. If an action isn’t specified, the page loads as usual. If the action method returns null, the page simply refreshes. This method is called before the page is rendered, and allows you to optionally redirect the user to another page.
Important: This action should not be used for initialization or DML.
<apex:page standardController="Case" extensions="EscalCase" action="{!caseEscalation}">
<apex:form>
<apex:inputHidden value="{!case.OwnerId}"/>
</apex:form>
</apex:page>
The above Visualforce page uses the standard controller of the Case object and extends its functionality. This will enable us to get the Case record details. The ID is by default accessible to the Apex class. We have to include any other values we want the Apex class to access as hidden fields as shown. The action attribute on the <apex:page> will call the apex class method and completes the processing.
public 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.