Newer Version Available

This content describes an older version of this product. View Latest

Processing Results

You can process the results of a query that returns enough rows to require queryMore and queryLocator, much as you do now, iterating across the results:

1var result = sforce.connection.query("select name, id from account");
2    var queryMore = true;
3    while (queryMore) {
4        var records = result.getArray("records");
5        for (var i = 0; i < records.length; i++) {
6            //process records[i]
7        }
8        if (result.getBoolean("done")) {
9            queryMore = false;
10        } else {
11            result = sforce.connection.queryMore(result.queryLocator);
12        }
13    }

However, the AJAX Toolkit provides the QueryResultIterator object so that you can easily iterate through results without invoking queryMore and queryLocator. If you are experienced with the API and JavaScript, see QueryResultIterator.

For other calls, you must handle the batching of up to 200 records at a time yourself. For example, the following sample shows how to batch files for a create() call:

1var accounts = [];
2
3for (var i=0; i<10; i++) {
4    var account = new sforce.SObject("Account");
5    account.Name = "my new account " + i;
6    accounts.push(account);
7    }
8
9var result = sforce.connection.create(accounts);
10
11var sb = "";
12
13for (var i=0; i<result.length; i++) {
14    if (result[i].getBoolean("success")) {
15    sb += "\n new account created with id " + result[i].id;
16    } else {
17    sb += "\n failed to create account " + result[i];
18    }
19}
20
21alert("Result : " + sb);

For more examples, see Synchronous Examples.