How to Call SOQL from Javascript in Salesforce



We may require often for calling the SOQL from Javascript. We are (Merfantz Technologies) implemented this kind of scenario to Many of our clients.

We just taking the Account object for fetching account records whether it is equal to “Merfantz Account”.

Step 1: Create Visualforce Page and add the below line for calling JS resource.

<script src="/soap/ajax/28.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/28.0/apex.js" type="text/javascript"></script>

Step 2: Use below code for Fetching the Account Records

<script type="text/javascript">
  function CallSOQLfromJavascript() 
  {
  sforce.connection.sessionId = ‘{!$Api.Session_ID}';
  var AccountName=” Merfantz Account”;
  var qr = sforce.connection.query(“SELECT id,name 
                                    FROM Account 
                                    WHERE Name='”+ AccountName +”‘”);
  var records = qr.getArray(“records”);
  if (qr.size == 0){alert(“No Record found.”);}
  else{
   for (var i=0;i<qr.records.length;i++)
   {alert(qr.records[i].Name);
  }
 }}
</script>

Step 3: Use below code for Calling Javascript method from Button

<input type=”button” onclick=”CallSOQLfromJavascript();” value=”Fetch Account Records”/>

By using the above-given code, you can query on any object or custom object of Salesforce without using the Apex code.

We are the ISV Partners and Please reach us for custom development => www.merfantz.com

——————————————————— Cheers !!!———————————————————


You must be logged into post a comment.