You need to sign in to do that
Don't have an account?
Warren
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.
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.
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
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.
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.
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.