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
somensomen 

difference between queryResult and queryResult.records.length

Hi all,
 
I am very new to salesforce, so this question might seem silly to a few.
 
i have a code that inserts  400 contacts into the Contact  table.
 
for(i=0;i<400;i++)
{
  var contact = new Sforce.Dynabean("Contact");
  contact.set("LastName","Gates"); 
  contact.set("Description","test contact number ");
  var saveResult = contact.save(); 
 }
 
now i ran a query to fetch all the contacts from the Contact table.
 
var queryResult= sforceClient.query("Select Id from Contact")
 
after fetching,
alert(queryResult)      returned 487 records as size.
 
but alert(queryResult.size) and alert(queryResult.records.length)    returned only 200.
 
can anybody explain the difference in the numbers?
 
Also when i transferred all the contacts into the CampaignMember table after assigning all of them to a campaign, the total number of contacts transferred was only 200.
 
Can someone tell me where i am going wrong?
 
 
Thanks in advance.
 
 
Ron HessRon Hess
You have 200 of the 487 records, you need to call queryMore() to get the rest, perhaps several times to get all.

look at queryResult.done to see if you need to call queryMore again.
DevAngelDevAngel
So, you are executing a save call 400 times in a row!!!  Dude!!

Not efficient.  All API crud calls work on arrays or in a batch mode, take advantage where you can.

var contacts = new Array();
for (var i=0;i<400;i++) {
    var contact = new Sforce.Dynabean("Contact");
    contact.set("LastName", "Gates");
    contact.set("Description", "test contact number");
    contacts.push(contact);
}
var saveResult = sforceClient.create(contacts);

There are two values when dealing with query results.  (By the way, this is all in the AppExchange API documentation).  One is the size field.  queryResult.size - this will give you the total number of records that matched your criteria.  The other is the length of the records array queryResult.records.length - this will give you the number of records contained in this response. 

Size and records.length may not match.  The reason is there is the concept of a batchSize for a query.  You can set this as high as 2000 or as low as 200.  200 is the default.  When they don't match, it means the number of records that match is greater than the current batch size.  This is a perfectly acceptable situation.

So, what to do if they don't match?  There is another value returned on the queryResult called the queryLocator.  This is essentially a cursor on the dataset.  You can use this "cursor" to retrieve the rest of your records by passing it in the queryMore call.  The typical structure to get all the records from a query, regardless of batch size is shown below.

Code:
var qr = sforceClient.query("Select Id From Account");

if (qr.size > 0) {
    var done = false;
    while (done == false) {
        for (var i=0;i<qr.records.length;i++) {
           //do something with qr.records[i]
        ]
        if (qr.done == true) {
            done = true;
        } else {
            qr = sforceClient.queryMore(qr.queryLocator);
        }
    }
} else {
    alert("Your query resulted in 0 records.");
}

 


bssantoshbssantosh

Thank you very much for all the replies.

It was exactly what I wanted.

The code generates the desired output now and executes much faster.

Thank you again.

somensomen

Thanks for all the help.

I got all the records returned through the query and the queries also execute faster now. 

Thanks one and all.

michaelforcemichaelforce

Yeah, I am a new guy as well, and I encountered this problem and learned about the 200 limit the hard way...

My related question is....  isn't the limit on the API 500 records per query?  Is the limit simply different when you are using the AJAX toolkit?

DevAngelDevAngel
The default is 200.  There is no difference in how the batch size works in AJAX versus other clients.