+ Start a Discussion

QueryResultIterator strange behavior

I'm not certain if this is a bug or the way I'm handling the results of QueryResultIterator, but I am not getting consistent results.  I'm using a simple relationship query to walk through Opportunity Line Items and the code returns different things depending on whether there is one or more than one line item.  You can see below, I'm referring to a TemplateID__c field which we use to control how some line items are created.  That field is critical to managing our line items and something has changed in the returns from the .next() call.  Here's what happens:
  • If the opportunity has 1 line item, then my lineItem variable will contain a flat record and I can reference the field as lineItem.TemplateID__c
  • If the opportunity has more than 1 line item it gets weird.  The first time through the loop, the lineItem variable is an array and I have to refer to the field as lineItem[0].TemplateID__c.  However the SECOND time through the loop it's flat!  And I can reference it as lineItem.TemplateID__c again.  So you can see the hack I inserted into the code.
Using Firebug I traced this back through and it is definitely the next() method that returns the flat vs. arrayed records.  This is repeatable, happens every time.  Took me forever this morning to figure out why my users were complaining.  FWIW this particular scontrol has been in place for months working fine until now.

strIdList = (list of opportunity Ids);
var result = sforce.connection.query("select o.Id, o.Name, o.CloseDate, o.HasOpportunityLineItem (select Id, PricebookEntryId, ServiceDate, TemplateID__c from OpportunityLineItems) from Opportunity o where o.Id in " + strIdList);
var oit = new sforce.QueryResultIterator(result);
while(oit.hasNext()) {
 var opp = oit.next();
 arrAllOpps[opp.Id] = opp;

...later processing each opportunity
var currentOpp = arrAllOpps[oppid];
//if (currentOpp.OpportunityLineItems) {
if (currentOpp.getBoolean('HasOpportunityLineItem')) {
 var lit = new sforce.QueryResultIterator(currentOpp.OpportunityLineItems);
 while(lit.hasNext()) {
  var lineItem = lit.next();
  var templateId = (lineItem.TemplateID__c == null) ? lineItem[0].TemplateID__c : lineItem.TemplateID__c;
  (...more operations on the line item)

One additional problem we had was the commented 'if' line.  That used to work, but lately I've had to change it to the getBoolean version.  Not a huge problem, but annoying that something changed.  Has anyone else seen something like this?

I guess no one else has seen this issue?