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
ElenaCaptaElenaCapta 

length of a list


Hi,
 
I want to know the length of a list, I'm use the next code. Does anybody know where is the error?
 
Thanks and best regards
Code:
<script src="http://www.salesforce.com/services/lib/ajax/beta3.3/sforceclient.js" type="text/javascript"></script> 
<script> 

function initPage ( ) { 
sforceClient.registerInitCallback (setupPage); 
sforceClient.init ( "{!API_Session_ID}" , "{!API_Partner_Server_URL_70}" ); 
} 

function setupPage ( ) { 
cuenta ='{!Account_Name}'; 
var queryResult = sforceClient.query("Select Id from Opportunity where Account =' "+cuenta+" ' and StageName = 'Ganada' ", false); 
var e =[]; 
e = queryResult; 
alert ('Test: '+e.length); 
} 

 
The_FoxThe_Fox
Hello Helena,

The exact syntax is e.size

Regards
ElenaCaptaElenaCapta

Thanks The_Fox,

but it isn't work still, do you know the cause??

Best regards, Elena

SteveBowerSteveBower
Hi, some new code:

Code:
function setupPage ( ) { 
var cuentaId ='{!Opportunity_Account_ID}'; 
var e = sforceClient.query("Select Id from Opportunity where AccountId ='" + cuentaId + "' and StageName = 'Ganada' ", false); 

alert ('Test: ' + e.records.length); 
// tambien
alert ('Test: ' + e.size);
alert(e);
} 

 
1. An Opportunity record doesn't actually have the Account *Name* stored in the Opportunity, it has the "AccountId".  This is where Salesforce is being helpful in the user interface, and displaying the Account Name for you, but it's not really there in the data.

So, The query is changed to use AccountId and  {!Opportunity_Account_ID}

2. "cuenta" needs a "var".

3. The Query is also changed to remove the spaces that you used to have around cuenta.

4. You don't need to create a different array to hold the results of the query, just one.  So, I removed "queryResult", and are just using "e".  The important thing to remember is that "e" is not an Array.  "e" is now of type "QueryResult".

5. "e" has several fields depending on the results of the query.  However, if it works properly it will have an array called records that holds your data and you can use the length() function on that.

6. "e" also has a .size field, however the size may not always be the same as the length depending on other factors like the BatchSize.

7. For debugging, if you aren't getting the values you expect, you can always just display the entire queryResult by just displaying the whole variable.   'alert(e);'

The code above should work but I haven't run it.   I would suggest going to the AppExchange and downloading some of the applications there.  Then explore the s-controls that others have written to get a feel for how it works, how to detect the various kinds of error conditions, and how to us the different database fields.

Best of luck, Steve Bower



ElenaCaptaElenaCapta
Hi Steve,
 
thanks for all but I don't obtain the good code, this is the code it should be easy but now I don't know what can I do?
Do you know what it can be happening?
thank you very much!! Elena
 
 
 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<script src="http://www.salesforce.com/services/lib/ajax/beta3.3/sforceclient.js" type="text/javascript"></script>
<script>

function initPage ( ) {
sforceClient.registerInitCallback (setupPage);
sforceClient.init ( "{!API_Session_ID}" , "{!API_Partner_Server_URL_70}" );
}

function setupPage ( ) {
var cuentaId ='{!Opportunity_Account_ID}';
var e = sforceClient.query("Select Name from Opportunity where AccountId ='"+cuentaId+"' and StageName = 'Ganada'", false);
alert('hola');
//alert ('Test: ' + e.records.length);
// tambien
alert ('Test: ' + e.size);
//alert(e);
}

</script>
</head>
</html>
<body onload="initPage()">
</body>
 
Best Regards!

Message Edited by ElenaCapta on 07-18-2006 09:32 AM

The_FoxThe_Fox
Hello Elena, When it does not work please be more precise on what happen (error message....) and what I would recommend is to take any Appexchange querying Opportunity and trying to understand the logic behind it. Nevertheless, what I STRONGLY recommend is to use Firefox for development of s-controls with the Firebug extension and eventually the web developper toolbar. You code for firefox that is the browser closer to W3C recommandations and then if needed you tweak for your IE users but not the other way round. Code:
var oppId = "{!Opportunity_ID}"; 

var qr_opp = sforceClient.Query( "Select Id, CloseDate from Opportunity where Id = '" +  oppId + "'" );
if (qr_opp.className != "QueryResult") {
  alert( qr_opp.className + "could not find the opportunity!" + qr_opp.toString()); return;
 } 
if (qr_opp.size > 0) {
 Opps[qr_opp.records[0].get("Id").slice(0,15)] =  qr_opp.records[0];
 }
That is working I have no simple s-controls at hand but do not hesitate t post again. Second thinking did you try your query in the sforce explorer just to be sure Regards  

Message Edited by The_Fox on 07-19-2006 09:15 AM

DevAngelDevAngel
Also, if you are not using the asynchronous call, remove the , false from the query statement.

var e = sforceClient.query("Select Id from Opportunity where AccountId ='" + cuentaId + "' and StageName = 'Ganada' "); 

ElenaCaptaElenaCapta

Thanks to all!!!

Now it's work OK!

Thanks for all and best regards!:smileywink: