function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nithesh NNithesh N 

How to call an Apex method from a Custom Button? (Not Javascript Button)

I want to call an apex method from a custom Button (Url type button) with some merge fields.
With lightning rolling out, I dont want to use Javascript button anymore. 

My Use-Case is to call an Apex static method which takes record id as parameter. 
Public static void methodToBeCalled(ID recordID) {
   //My Code Logic Here
From some blogs, I found that i can use REST APIs to do this, But no idea on how to implement it.
So, Please provide me any code sample or any pointers, I appreciate any help.
Best Answer chosen by Nithesh N
Alba RivasAlba Rivas

For making a REST API callout you would need a javascript button. URL hacking doesn't work in Lightning Experience either, as you can see in the documentation (

My advice here is: create a custom button that redirects to a Visualforce page. In that visualforce page, create a confirmation button that calls your apex code. 
<apex:page standardController="Account" extensions="MyPageController">
	    <apex:commandButton action="{!method}" value="Are you sure?"/>
public class MyPageController {

	private String recordId;

    public MyPageController(ApexPages.StandardController controller) {
        recordId = controller.getRecord().Id;
 	public void method() {
    // Do whatever with recordId

Bear in mind you should specify the sObject you are using in the standardController parameter (I used Account as an example).

If your method is not performing DML, it is safe too to use the action attribute on page load ( However, if your method is performing DML, don't use it, because you would be creating a CSRF vulnerability.