Migrate sforce.connection.query to Lightning equivalent

Reading Time: 2 minutes

As you probably know, JavaScript buttons are not supported in Lightning Experience (LEX). In particular, you can’t use REQUIRESCRIPT.

In this post, the idea is to show how to migrate a particular JavaScript button that uses sforce.connection.query sentence.

Suppose our JavaScript button is like this:

      
{!REQUIRESCRIPT("/soap/ajax/37.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/37.0/apex.js")} 
 
var query ="SELECT Id, Name FROM Account LIMIT 10";
var queryRecords= sforce.connection.query(query);
var records = queryRecords.getArray("records"); 
//Do something with the records...
      
    

A Lightning equivalent solution requires several steps. I assume that you have some knowledge about Lightning Components development as well as Apex development.

Well, let’s go!

Create a Lightning component called MyQueryResult and Its JavaScript controller

MyQueryResult.cmp

      
<aura:component controller="MyQueryResultService" >
    <aura:attribute name="queryResult" type="SObject[]" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
  
    <!-- It just displays the results. Modify It depending on your needs -->
    <aura:iteration items="{! v.queryResult}" var="item">
        {!item.Id} - {!item.Name}<br/>
     </aura:iteration>
</aura:component>
      
    

MyQueryResult.js

      
({
    doInit : function(component, event, helper) {
        var myQuery = 'SELECT Id, Name FROM Account LIMIT 10';
        var action = component.get("c.executeQuery");
        action.setParams({
            "theQuery": myQuery
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if(state == "SUCCESS" && component.isValid()){
                console.log("success") ;
                var queryResult = response.getReturnValue();
                console.log(queryResult);
                component.set("v.queryResult", queryResult);
            }else{
                console.error("fail:" + response.getError()[0].message); 
            }
        });
        $A.enqueueAction(action);
    }
})
      
    

We will need also a service (an Apex class) to execute our query

MyQueryResultService

      
public class MyQueryResultService {
 
    @AuraEnabled
    public static List<sObject> executeQuery(String theQuery){
        try{
            String query = String.escapeSingleQuotes(theQuery);
            return Database.query(query);
        }catch(Exception e){
            throw new AuraHandledException('Error doing the query: '+theQuery+' Error: '+e.getMessage());
        }
    }
}
      
    

After that, we need a quick action pointing to our component.

The last step is to add our quick action to the layouts we need

Read more about this at Lightning Alternatives to JavaScript Buttons

Photo by Glenn Carstens-Peters 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: