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
JEndoJEndo 

Flex Toolkit: QueryResultIterator exhibits odd behaviour

I'm noticing some very odd behaviour with the QueryResultIterator in the Flex toolkit-  It seems to, every so often, think there's an extra record in the query result, and so calls the forEach function one extra time with a null record, resulting in a null object reference.  I can't seem to find a consistent way to replicate this behaviour, but here's an overview of what I'm seeing:

The query I most often see this happen on returns a single result- it's actually querying the Attachments, and looks something like this:

SELECT Id, Body FROM ATTACHMENT WHERE Name='<someName>' AND ParentId='<someId>'

Now, this query should only return a single result.  The Java toolkit returns a single record, the DataLoader returns a single record, and looking manually at the attachments in this org, it should only return a single result.  However, when debugging, the call to lastSObject() returns false after processing the first record.  Further debugging shows this is because the QueryResult for the Iterator actually thinks there are 2 records, with the second element in the records collection being a null object.

Debugging the QueryResult class itself always returns a correct QueryResult, so I'm not really sure where this could be coming from.  Has anyone else noticed anything of this sort?

On a side note, I made a small change to the code in the DescribeSObjectResult class that might be helpful to others-  because of the way we were adding fields/child relationships/record types to their respective Arrays, the .length attribute was always 0.  I found it necessary in one spot to know the number of fields returned on a describe, so I added an explicit increment to the array.length each time I processed a field.  It's a little bit of a hack, but it works.  Code follows:

Code:
public function DescribeSObjectResult(obj:ObjectProxy) {
for (var key:String in obj) {
var val:Object = obj[key];
if (val is ArrayCollection || val is ObjectProxy) {
if (key == "fields") {
var fieldArray:Array = new Array();
for (var i:int = 0;i<(val as ArrayCollection).length;i++) {
var field:Field = new Field((val as ArrayCollection)[i]);
fieldArray[field.name] = field;
fieldArray.length++;
}
this[key] = fieldArray;
} else if (key == "childRelationships") {
var crArray:Array = new Array();
var cr:ChildRelationship;

if ( val is ObjectProxy ) {
cr = new ChildRelationship(val as ObjectProxy);
crArray[cr.relationshipName] = cr;
crArray.length++;
} else {
for (var i2:int = 0;i2<(val as ArrayCollection).length;i2++) {
cr = new ChildRelationship((val as ArrayCollection)[i2]);
crArray[cr.relationshipName] = cr;
crArray.length++;
}
}
this[key] = crArray;
} else if (key == "recordTypeInfos") {
var rtArray:Array = new Array();
var rt:RecordTypeInfo;

if ( val is ObjectProxy ) {
rt = new RecordTypeInfo(val as ObjectProxy);
rtArray[rt.name] = rt;
rtArray.length++;
} else {
for (var i3:int=0;i3<(val as ArrayCollection).length;i3++) {
rt = new RecordTypeInfo((val as ArrayCollection)[i3]);
rtArray[rt.name] = rt;
rtArray.length++;
}
}
this[key] = rtArray;
}
} else {
this[key] = obj[key]
}
}
}

 



JEndoJEndo
Doing a little more experimenting, it seems this query (and others build similarly) will almost always exhibit this behaviour:

Code:
SELECT Id, Name, Body FROM ATTACHMENT WHERE ((Name like 'lock%.txt' OR Name='descriptor.xml') AND ParentId='a0070000009eX6cAAE')