Friday, 21 March 2014

Processing results obtained from API calls in Javascript

We can get the results by using these two API calls:


















  • Query()
    • QueryResult = sforce.connection.query(string queryString);
  • QueryMore()
    • QueryResult = sforce.connection.query(QueryLocator queryLocator);

Executes a query against the specified object and returns data that matches the specified criteria.

QueryResult has following 4 properties:
    • queryLocator
    • done
    • records
    • size

Ponits to remember

  • Default query result size = 500 rows
  • If the query results exceed 500 rows, queryMore() call and a server-side cursor(queryLocator) to retrieve additional rows in 500-row chunks.
  • You can increase the default size up to 2,000 in the QueryOptions header.
    • connection.setQueryOptions(250);


queryMore() and queryLocator

  1. var result = sforce.connection.query("select name, id from account");  
  2. var queryMoreFlag = true;  
  3. while (queryMoreFlag) {  
  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.           queryMoreFlag = false;  
  10.      } else {  
  11.      result = sforce.connection.queryMore(result.queryLocator);  
  12.      }  
  13. }  

QueryResultIterator()

The AJAX Toolkit provides the QueryResultIterator object so that you can easily iterate through results without invoking queryMore and queryLocator.

  1. var result = sforce.connection.query("select id, name from account");  
  2.      var it = new sforce.QueryResultIterator(result);  
  3.      while (it.hasNext()) {  
  4.           var account = it.next();  
  5.           sforce.debug.log(account.Name);  
  6.     }  


Quick Link to parent document: Working with the AJAX Toolkit

No comments:

Post a Comment

Thank you for visiting. Your comments are highly appreciated.