Salesforce: Call Apex Class on Click of Custom Button

|
| By Webner

1. Create apex class that you want to call (it should be declared global):

Global class Test
{
Webservice static void testMethod()
{
//do something.
}
}

Note: Webservice method must be static:

Global class Test
{
Webservice static void testMethod(String IDD)
{
//do something.
}
}

2. Goto–>Setup–>Object–>Buttons, links and Actions section

Click on New button or Link:

i) Enter Name of the button
ii) Behaviour: Execute Javascript
iii) Content source:On-Click javascript
iv) Display Type:Detail Page Button

Javascript code:

Case1: No need to pass Parameters:

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
 
sforce.apex.execute("Test","testMethod",{});
window.location.href="/{!object.Id}";

Case2: When we need to pass parameters:

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
 
sforce.apex.execute("Test","testMethod",{"IDD":'{! object.Name }'});
window.location.href="/{!object.Id}";

Note: Parameter name should be same as apex class:
1

Leave a Reply

Your email address will not be published. Required fields are marked *