Activity Forums Salesforce® Discussions In Salesforce, how can we execute query in javascript function?

  • In Salesforce, how can we execute query in javascript function?

     William updated 3 years ago 3 Members · 3 Posts
  • sushant

    December 14, 2016 at 1:59 pm

    Hi All,

    How can we execute query in javascript function?

    please give suggestions


  • Vikas Kumar

    January 16, 2017 at 7:08 am

    Hi Sushant,

    SOQL query in javascript example
    You can use SOQL in java-script on your VF pages or any kind of java-script that you write, like we can get it executed on click of a button or link present on your detail page of a record. Below is the simple example and you can use it and modify it accordingly :

        var query = “SELECT Id,Name from Account LIMIT 2″;
        var records = sforce.connection.query(query);
        var records1 = records.getArray(‘records’);
        var accountNames = ”;
        for(var i=0;i<records1.length;i++){
            accountNames = accountNames + records1[i].Name + ‘,’;
        if(records1.length == 1){
            //window.location.href = ‘’;
        else {
            alert(‘There is no Account’);
        alert(‘An Error has Occured. Error:’ +e);

    You need to use .js files that are in first two lines in order to use the api of salesforce to connect and fetch the records using SOQL. In the example you will see result of SOQL in alert statement. The result that is returned contains a ‘records’ named array component that can be used to iterate over and go through all the records and use it in the same manner as we do in usual apex program. For ex, account.Name etc.

    You can also use merge fields to create dynamic queries.

    Similarly you can use javascript to create, update or delete the salesforce object’s records using API. Below is the sample code you can use to create a new account record in your org.

        var accounts = [];
        var account = new sforce.SObject("Account");
        account.Name = "my new account Test";
        var results = sforce.connection.create(accounts);
        if (results[0].getBoolean("success")) {
            alert("new account created with id " + results[0].id);
        } else {
            alert("failed to create account " + results[0]);
    alert('An Error has Occured. Error:' +e);
  • William

    November 12, 2018 at 4:31 am

    Try this one -

    <apex:page id=”page”>
        <script src=”/soap/ajax/38.0/connection.js” type=”text/javascript”>
        function show(){
            var queryResult=sforce.connection.query(‘select name,amount,closeDate,stageName from Opportunity’);
            var records=queryResult.getArray(‘records’);
            var result=”;
            var amt=0;
            for(var i=0;i<records.length;i++){
        <apex:form id=”fm”>
            Click to See Opportunity Names
            <apex:commandButton value=”Submit” oncomplete=”show()” />
            <apex:outputLabel id=”res”/>
Viewing 1 - 3 of 3 posts

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos