function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bssantoshbssantosh 

Problems in retrieving records from sales force database using Ajax

Hi;

      I want to query the sales force database using Ajax for which I have used following code.

    In this case I can sucessfully login and and code is working without any script errors but every time I get zero records from database(Refer alert message highlighted in red in the code below).What could be the reason for this?

 

<html>

    <head>

        <script language="javascript" src="https://www.salesforce.com/services/lib/ajax/beta3.3/sforceclient.js" type="text/javascript"></script>

        <script>

            <!--

            function initPage() {

                sforceClient.registerInitCallback(setup);

                sforceClient.init("session_id", "server_url", true);

            }

            //Use this function as the entry point for your DHTML and JAVASCRIPT processing

            function setup() {

                var queryResult = sforceClient.query("Select Id, Name, Industry From Account", layoutResults);

            }

            function layoutResults(queryResult) {

                if (queryResult.className == "Fault") {

                    alert("There was an error: " + queryResult.toString());

                } else {

                    alert(queryResult.records.length)//This always gives value 0

                    if (queryResult.size > 0) {

                        var output = "";

                        for (var i=0;i<queryResult.records.length;i++) {

                            var dynaBean = queryResult.records[i];

                            output += dynaBean.get("Id") + " " + dynaBean.get("name") + " [Industry - " + dynaBean.get("Industry") + "]<br>";

                        }

                        var textNode = document.createTextNode(output);

                        document.getElementById("output").innerHTML = output;

                    } else {    

                        var textNode = document.createTextNode("No records matched.");

                    }

                }

            }

            //-->

        </script>

    </head>

    <body onload="initPage()">

        <div id="output"></div>

    </body>

</html>

Gareth DaviesGareth Davies

You might want to post this on:

http://forums.sforce.com/sforce/board?board.id=ajax_toolkit

Code:

sforceClient.query("Select Id, Name, Industry From Account where id!='1'", layoutResults); 


 

What happens if you put a where condition in the call, see above?
DevAngelDevAngel
Move your alert down one line to be inside the block that checks the size attribute.  The pattern for queries, regardless of platform or language, is to run the query, check the size, if the size is more than 0 then iterate the results.