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
WarrenWarren 

AJAX Query problem

I am having a problem with a query within an AJAX S Control. The query returns a few records but I am at a loss to work out how to loop through the result to extract data from the fields within the query.


Here is a code snippet:

[script language="javascript" src="https://www.sforce.com/ajax/beta2/sforceclient.js" type="text/javascript"]

[/script]
[script type="text/javascript" language="JavaScript"]
[!--

var dongleID= "{!vicon_dongle_ID}";

alert(dongleID);
sforceClient.init("{!API_Session_ID}", "{!API_Enterprise_Server_URL_60}");

sforceClient.setBatchSize(10);


function doQuery(){
var saveResult = sforceClient.Query("Select dongle_option__c, Id, Name, vicon_dongle__c from dongle_lic__c where vicon_dongle__c='" + dongleID+"'");

if (saveResult.getClassName() == "QueryResult") {
alert("saveResult="+saveResult)

for(c=0;c[saveResult.size;c++) {
sr = saveResult[c];


//This next line does not work
// alert(sr.get("vicon_dongle__c"));

alert(c);
}//end of for loop on the save result.


return saveResult.records;
}//end of if

}
doQuery();

//--]
[/script]

Any help would be greatly appreciated.
DevAngelDevAngel
One thing to constantly keep in mind, the AJAX Toolkit uses the API and closely matches how you use the api. In the case of the queryResult (as documented in the API doc), an array of records is included in the object. This is the actual data that you requested. You need to loop through that array to access individual record data.
WarrenWarren
I apologise but I am a bit confused here. In all examples of S Controls that I have looked at that use the AJAX toolkit, this apporach and syntax seems to work. Take the oppConvertLineItems sample:

var oli = sforceClient.Query("select id, PricebookEntryId from OpportunityLineItem where OpportunityId = '{!Opportunity_ID}' ");
if (oli.size < 1) { return ""; }
// build a list of pricebook entry ID's to retreive product ID's
var pbeid = new Array();
for(var i = 0; i < oli.size ; i++) {
pbeid.push(oli.records[i].get("PricebookEntryId"));
}


Here the Array push method is being used to add the PricebookEntryId string to new elements in the pbeid array.

What seems to be failing with my code is the queryresult.records[i].get([FieldName from SOQL query]). Should this not return a string for a valid i and FieldName

Message Edited by Warren on 02-08-2006 06:03 AM

DevAngelDevAngel

Using queryResult.size in your loop is not reliable.  The size field lets you know if any records matched the query, and if so how many (and here's the important part) at the time of your query.  What this means is that you could have a size of 1000 matching records, and in the process of working through all 1000, another user may delete a record.  That is why you should always use queryResult.records.length as the iterator limit in a for.


Warren wrote:
I apologise but I am a bit confused here. In all examples of S Controls that I have looked at that use the AJAX toolkit, this apporach and syntax seems to work. Take the oppConvertLineItems sample:

var oli = sforceClient.Query("select id, PricebookEntryId from OpportunityLineItem where OpportunityId = '{!Opportunity_ID}' ");
if (oli.size < 1) { return ""; }
// build a list of pricebook entry ID's to retreive product ID's
var pbeid = new Array();
for(var i = 0; i < oli.size ; i++) {
pbeid.push(oli.records[i].get("PricebookEntryId"));
}


Here the Array push method is being used to add the PricebookEntryId string to new elements in the pbeid array.

What seems to be failing with my code is the queryresult.records[i].get([FieldName from SOQL query]). Should this not return a string for a valid i and FieldName

Message Edited by Warren on 02-08-2006 06:03 AM


The correct code for iterating a query result should look more like this:

for (var i=0;i<oli.records.length;i++) {...}

You have the correct syntax for getting the field value. 

Ron HessRon Hess
good point Dave, it looks like i wrote the sample mentioned, and will now have to change my ways...
Ron HessRon Hess
Warren, your .get(fieldname) will only work if the field name was mentioned in the SOQL statement.

not all fields are returned, only those listed in the SELECT string, so given this string:
select id, PricebookEntryId from OpportunityLineItem

you can only do
.get("Id") OR .get("PricebookEntryId")

the dynabean will not contain data in any otherwise valid field names.