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
Brian KoblenzBrian Koblenz 

query return not providing q.records.length

I feel a bit stupid.  I have a pretty straightforward javascript custom button where I am trying to do a query and then iterate over the resulting set of objects.

My broken code is:
query_str = 'Select Id, ' + 
                'RecordType.Name, QB_Propagated__c ' +
                'FROM Opportunity WHERE ' +
                  'QB_Propagated__c=false AND ' + 
                  'RecordType.Name=\'Donation\' AND ' + 
                  'Id IN (\'' + idArray.toString().replace(/,/g, "','") + '\')';
alert(query_str);
ores = sforce.connection.query(query_str);
alert(ores);
alert('len: ' + ores.records.length);

The ores.records is defined and appears to be a list of entities that I want to iterate over (an alert(ores.records) gives me an object that starts {type:.....,} ), but my alert referencing the length of ores.records is undefined.

If I use the same query but do NOT include the AND ID IN ('sfid1','sfid2') in the query then the ores structure looks the same (to my eye!) but the code does work and I can refer to ores.records.length and get something meaningful.

In the "failing" version there is only one record that satisfies the condition.  If I extend my test so that two records satisfy my condition then I get the expected behavior.

Is it possible that I need two versions of code to deal with <=1 record returned from the query and >1 record?  I was hoping to just iterate over the length of the list.

-brian
 
Best Answer chosen by Brian Koblenz
BalajiRanganathanBalajiRanganathan
Try to use
var records = result.getArray("records");

and then use records.length instead of ores.records.length
 

All Answers

BalajiRanganathanBalajiRanganathan
Try to use
var records = result.getArray("records");

and then use records.length instead of ores.records.length
 
This was selected as the best answer
Brian KoblenzBrian Koblenz
You're my hero!  Does query return an array if more than one, just the element if exactly one and nothing otherwise.  Is that what getArray paper's over?
BalajiRanganathanBalajiRanganathan
Thats correct. you can view that in the connection.js source file