How to convert sforce.apex.execute to Lightning

Reading Time: 2 minutes

Let’s see how to convert a Salesforce JavaScript button that uses sforce.apex.execute in to Lightning Experience.

The good news is you will reuse this code and the approach for all your similar buttons

Imagine you have this JavaScript button in your Salesforce instance. OK, probably your button is larger and more complex but the idea is the same

      
{!requireScript("/soap/ajax/20.0/connection.js")} 
{!requireScript("/soap/ajax/20.0/apex.js")}
 
sforce.apex.execute("JSMMyExecuteClass", "myExecuteMethod", {param1:"Im param1",param2:"Im param2"}); 
window.location = 'https://google.com';
      
    

In the example above we want to invoke the method myExecuteMethod that belongs to the Apex class JSMMyExecuteClass and you will pass these parameters {param1:”Im param1″,param2:”Im param2″}

After that, it redirects to google.com

There’s not a direct solution (aka point and click) so we have to work a little bit

Create a Lightning Component

JSMMyButton.cmp

      
<aura:component implements="force:lightningQuickAction" controller="JSMMyExecuteClass" >
 
  <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
 
</aura:component>
      
    

Create a controller

The controller part of my component is called JSMMyButton.js

      
({
  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);
  }
})
      
    

Create Apex class

In JSMMyExecuteClass we need to create an equivalent method to call from our js

      
public class JSMMyExecuteClass {
 
  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);
  }
}
      
    

Create a Quick Action

Create a quick action that points to our component

Add the quick action to the layout(s) you want

That’s all. You can use this approach again for others button you have that require some code.

Video

Maybe this video is interesting for you: Journey to Lightning Experience: Convert Your JavaScript Buttons

Photo by Artem Sapegin on Unsplash


About the author

Andrés Canavesi
Andrés Canavesi

Software Engineer with 15+ experience in software development, specialized in Salesforce, Java and Node.js.


Related posts


Leave a Reply

%d bloggers like this: