Loading

How to Call Apex Class Methods from a Custom Button or Link Using Visualforce

Veröffentlichungsdatum: Jun 20, 2026
Beschreibung

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.

Lösung

Step 1: Create the Visualforce Page

Create a Visualforce page that uses the standard controller of the target object (for example, Case) and an Apex extension class. Set the action attribute to reference the Apex method to be called on page load.
Key elements of the Visualforce page:

  • standardController="Case" — binds the page to the Case object so that the Case record ID is automatically available
  • extensions="EscalCase" — references the Apex class that extends the standard controller
  • action="{!caseEscalation}" — calls the caseEscalation() method in the Apex extension class when the page loads
  • The <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>

Step 2: Create the Apex Controller Extension

The Apex extension class receives the standard controller in its constructor, retrieves the current Case record, and contains the business logic in the action method.
Key elements of the Apex class:

  • The constructor EscalCase(ApexPages.StandardController controller) accepts the standard controller and retrieves the Case record
  • The caseEscalation() method contains the custom business logic and returns a PageReference to redirect the user back to the Case record page
  • The method uses setRedirect(true) on the PageReference to ensure a proper page redirect after 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
    }
}

Step 3: Add the Visualforce Page as a Custom Button

  1. Go to Setup | Object Manager | Case | Buttons, Links, and Actions
  2. Click New Button or Link
  3. Set the Display Type to Detail Page Button
  4. Set the Content Source to Visualforce Page
  5. Select the Visualforce page created in Step 1
  6. Add the button to the Case Page Layout
Nummer des Knowledge-Artikels

000385214

 
Laden
Salesforce Help | Article