How to Convert sforce.apex.execute to Lightning | Salesforce Developer Guide
Introduction
In this blog, we will learn how to directly call the apex class through a custom button or javascript button on the object detail page and how we can convert how we can do the same thing in Lightning.
Steps to Create Button
- Go to Setup first, click Object Manager, then click Account.
- Click Buttons, Links, and Actions, then click New Button or Link.
- Name the button
- Select Detail Page Button.
- Select Behaviour execute javascript and content source onclick javascript
- Write the javascript code and save.
For this, we must remember some things before writing code or creating custom buttons:-
- The Apex Class must be declared as Global which we want to call inside javaScript code.
- Method should be webservice static method inside the class.
- The class must be declared as Global.
Syntax of Apex Class
// Non-parameterized
Global class ExampleApexClass{
Webservice static void Method() {
//write your logic here.
}
}
// Parameterized Method
Global class ExampleApexClass{
Webservice static void testMethod(String objId){
//write your logic here.
}
}
Don't forget to check out: Using Batch Apex | Apex Developer Guide | Salesforce
Javascript code Syntax for Non-parameterized Method
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
sforce.apex.execute("Class_Name","Method_Name",{});
window.location.href="/{!object.Id}";
JavaScript code Syntax for parameterized Method
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
sforce.apex.execute("Class_Name","Method_Name",{objId: "{!object.Id}"});
window.location.href="/{!object.Id}";
Create a Lightning Component
sforce.apex.execute("Class_Name","Method_Name",{objId: "{!object.Id}"});
window.location.href="/{!object.Id}";
<aura:component implements="force:lightningQuickAction" controller="JSMMyExecuteClass" >
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
</aura:component>
Create a controller
({
doInit : function(component, event, helper) {
var action = component.get("c.myExecuteMethodAura");
action.setParams({
"param1": 'Im param1',
"param2": 'Im param2'
});
action.setCallback(this, function(response) {
var state = response.getState();
if(state == "SUCCESS" && component.isValid()){
console.log("success") ;
var result = response.getReturnValue();
console.log(result);
var urlRedirect = "https://www.google.com/search?q="+result;
var urlEvent = $A.get("e.force:navigateToURL");
urlEvent.setParams({
"url": urlRedirect
});
urlEvent.fire();
}else{
console.error("fail:" + response.getError()[0].message);
}
});
$A.enqueueAction(action);
}
})
Check out another amazing blog by Kirandeep here: Learn All About Platform Events in Salesforce | The Developer Guide
In MyExecuteClass we need to create an equivalent method to call from our js
public class MyExecuteClass {
public String myExecuteMethod(String param1, String param2){
return 'ok '+param1+' - '+param2;
}
@AuraEnabled
public static String myExecuteMethodAura(String param1, String param2){
return new JSMMyExecuteClass().myExecuteMethod(param1, param2);
}
}

Responses