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
LearnerrrLearnerrr 

how to call class from custom button (Created in pagelayout)??

Best Answer chosen by Learnerrr
Ajvad AjuAjvad Aju
Hi Sweta,

In classic,
For calling apex class from custom button the class must be global.
You can create custom button from salesforce org using javascript.
Following the steps,
After creating apex class (global).
Goto --> Setup --> Object --> Buttons, links and Actions section
Click New Button or Link
Enter the Name of the button
Behaviour : Execute Javascript
Content source : On-Click Javascript
The sample js code for the button is
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

if({!AAA__c.Name}!=Null)
{
    sforce.apex.execute("MyClass","myMethod",{}"});
    alert("This is {!AAA__c.Name}");
}

In Lightening
Create a lightning component that implements force:lightningQuickAction.
We can override our action using these component.
Then we can call apex class by seting our client side controller to a callback function.
Use this code
var action = cmp.get("c.YourMethodName");
action.setParams({
     param1: cmp.get("v.something1"),
     param2: cmp.get("v.something2")
});
action.setCallback(this, function(response) {
   if (response.state === "SUCCESS"){
       var serverResponse = response.getReturnValue();
   }    
});
$A.enqueueAction(action);

Regards
Ajvad Aju

All Answers

Raj VakatiRaj Vakati
If its a standard one you cannt call direcly ..  there are the options 
  1. If your button is updaing any fields , you can call the apex class from the process builder or trigger 
  2. OR create custome visualforce page or lightning component 

 
Raj VakatiRaj Vakati
You can able to call the apex class form  visualforce page or lightning component  f
LearnerrrLearnerrr
I have created custom button to call apex class using javascript.
 
Raj VakatiRaj Vakati
Yes .. that you can do it but That will not work in lightning .. 
Ajvad AjuAjvad Aju
Hi Sweta,

In classic,
For calling apex class from custom button the class must be global.
You can create custom button from salesforce org using javascript.
Following the steps,
After creating apex class (global).
Goto --> Setup --> Object --> Buttons, links and Actions section
Click New Button or Link
Enter the Name of the button
Behaviour : Execute Javascript
Content source : On-Click Javascript
The sample js code for the button is
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

if({!AAA__c.Name}!=Null)
{
    sforce.apex.execute("MyClass","myMethod",{}"});
    alert("This is {!AAA__c.Name}");
}

In Lightening
Create a lightning component that implements force:lightningQuickAction.
We can override our action using these component.
Then we can call apex class by seting our client side controller to a callback function.
Use this code
var action = cmp.get("c.YourMethodName");
action.setParams({
     param1: cmp.get("v.something1"),
     param2: cmp.get("v.something2")
});
action.setCallback(this, function(response) {
   if (response.state === "SUCCESS"){
       var serverResponse = response.getReturnValue();
   }    
});
$A.enqueueAction(action);

Regards
Ajvad Aju
This was selected as the best answer
LearnerrrLearnerrr
Thanks Ajvad ..