+ Start a Discussion
trishtrish 

undefined message when there is only 1 record in a custom object

Hello,
 
I am trying to retrieve the records from a custom object dynamically and display it in S-Control as a list. My code works as long as there are more than 1 record, the query works correctly. However, if there is only 1 record, then "for" loop is skipped in the following code snippet is skipped. Value "qr.records.length" is set to undefined. Am I doing something wrong here?
 
var qr = sforce.connection.query("SELECT ID, Contact__c from CCList__c");
   for(i=0;i<qr.records.length;i++)
   {
     ....... Logic for displaying the record.....
   }
 
 
werewolfwerewolf
I'm not sure what exactly is wrong with your code there, but may I suggest that you use a QueryResultIterator instead?


SteveBowerSteveBower

You get a different structure in qr when there is only one record returned; not an array with just one entry, but the entry itself.

Throw a Try/Catch around the for loop, I suspect you're dying with an error to the effect that qr.records doesn't exist. 

Use:
      var records = qr.getArray('records');  

to "normalize" it into an array you can use. Also, you might also look into using the
QueryResultIterator object instead of rolling your own. It supports an implicit queryMore
call, etc.
-Steve

trishtrish

Thank you werewolf and SteveBower. The queryResultIterator worked!!!