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
AlexCRMmanagerAlexCRMmanager 

[queryresult].records.length returning 'undefined' for query result with one record

When I try to run the following code on an Opportunity that has only a single Opportunity Line Item product, my for loop doesn't execute because '[queryresult].records.length' comes back as 'undefined'.

However, if I run the code on an Opportunity that has more than one Line Item, it all works fine, and the for loops execute properly. Am I doing something wrong, or is this a bug in the Ajax Toolkit method 'records.length'?

Code:
var oppLineItemQuery = sforce.connection.query("Select Id, OpportunityId, PricebookEntryId, ServiceDate, ListPrice, UnitPrice, Quantity from OpportunityLineItem where OpportunityId = '" + parentOppId + "'"); 

oppLineItemList = oppLineItemQuery.records; 

document.write(oppLineItemQuery.records.length+"<BR>"); 

var examOrderItemArray = new Array(); 
for(var i = 0;i<oppLineItemQuery.records.length;i++) 
{ 
oppLineItems = oppLineItemList[i]; 
var examOrderItem = new sforce.SObject("OpportunityLineItem"); 

examOrderItem.set("OpportunityId",examOppID); 
examOrderItem.set("PricebookEntryId",oppLineItems.get("PricebookEntryId")); 
examOrderItem.set("UnitPrice",oppLineItems.get("UnitPrice")); 
examOrderItem.set("Quantity",'1.0'); 

examOrderItemArray.push(examOrderItem); 
} 

document.write(examOrderItemArray) 

try { 
var examOrderItemSaveResults = sforce.connection.create(examOrderItemArray); 
} 
catch(error) 
{ 
sforce.debug.log(error.faultcode); 
sforce.debug.log(error.faultstring); 
}

 

cheenathcheenath
Instead of:
oppLineItemList = oppLineItemQuery.records;
you have to use:
oppLineItemList = oppLineItemQuery.getArray("records");


Ajax toolkit 8.0 does not make a describe call to get metadata  before  doing a query
(that is why it is much faster than  the old  toolkit).  Anyway, this means toolkit
doesnt know that "records" is an array. If it finds more than one records element
in the XML response then it knows  that records is an array and it returns an
array.

If you say getArray() then the toolkit knows that you want an array and it always
returns an array. Since there is no type in the javascript world, you have to know
out of bound that records is an array. So requiring getArray() call is a better
trad off than making a describe call to get  meta data.

HTS,

 

Message Edited by cheenath on 02-16-2007 12:59 AM

Message Edited by cheenath on 02-16-2007 01:00 AM

AlexCRMmanagerAlexCRMmanager
Hi cheenath,

Thanks for the suggestion, it worked great! After I required the method to return an array and I modified the reference in the For loop, everything executed correctly.

Thanks again for the quick response!!

Here is the modified code:

Code:
var oppLineItemQuery = sforce.connection.query("Select Id, OpportunityId, PricebookEntryId, ServiceDate, ListPrice, UnitPrice, Quantity from OpportunityLineItem where OpportunityId = '" + parentOppId + "'"); 

// document.write(oppLineItemQuery.records); 

oppLineItemList = oppLineItemQuery.getArray("records"); 

var examOrderItemArray = new Array(); 
for(var i = 0;i<oppLineItemList.length;i++) 
{ 

oppLineItems = oppLineItemList[i]; 
var examOrderItem = new sforce.SObject("OpportunityLineItem"); 

examOrderItem.set("OpportunityId",examOppID); 
examOrderItem.set("PricebookEntryId",oppLineItems.get("PricebookEntryId")); 
examOrderItem.set("UnitPrice",oppLineItems.get("UnitPrice")); 
examOrderItem.set("Quantity",'1.0'); 

examOrderItemArray.push(examOrderItem); 
} 

// document.write(examOrderItemArray) 

try { 
var examOrderItemSaveResults = sforce.connection.create(examOrderItemArray); 
} 
catch(error) 
{ 
sforce.debug.log(error.faultcode); 
sforce.debug.log(error.faultstring); 
}