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
JohanButsJohanButs 

AJAX - Query method: Stopping my For Loop

In this code below i get a weird behaviour with my for loop. The code i commented and labeled 'THIS CODE SKIPS MY LOOP ONCE' is the section that gives the problem.

What i wanted to do is the following: i have one div with an id and when i click that div a section with items related to that div opens. The items related to that div have a checkbox, which must be checked if a certain record is found in the data tabel (Node_Item = custom object). The query works fine and it works. But it always skips my loop once.

example: i have a div with 4 related items. So i click the div and
- parentobj.childNodes.length = 4 OK
- i = 0
- it goes to the first item in the loop (parchild)
- it goes all the way down to the query because it's valid and it check's the checkbox because a record was found in the table

but then comes the weird thing: when i echo i in the next loop it says 2, so it skipped 1

Is this a problem in the toolkit or my code because i don't use a break or continue statement and when i comment the query code the loop works fine.

Can anybody help me ?



function setChecks(parentobj) {

for (i=0;i
var parchild = parentobj.childNodes[i];
strparchild = new String(parchild.id);
//EXCLUDE _ITEMS SECTION
if (strparchild.search('_ITEMS') != -1) {
//IS EEN COLLECTIE SECTIE
} else {
if (parchild.getAttribute('hasitems') == 'yes') {

} else {
//THIS CODE SKIPS MY LOOP ONCE
window.alert(parchild.id);
//var qr = sforceClient.Query("Select Id from Node_Item__c Where Account__c = '{!Account_ID}' And Path__c = '" + parchild.id + "'");
//if (qr.size == 1) {
//check=document.getElementById("C_" + parchild.id);
//check.checked = true;
//check.setAttribute("rec_id", qr.records[0].get("Id"));
//}
}
}
}
}
DevAngelDevAngel
Hmm..

Looks like something is causing one of the if statements to evaluate other that true.

Maybe it's your hasitems attribute. How is the div originally generated?
JohanButsJohanButs
I really tested the code thoroughly and it works fine (show alert boxes for every item) but when i unquote the query method then it goes all wrong and it stops the loop after 1 succesfull query is executed.

I also posted a more detailed explenation of my problem in the Customforce & multipforce forum.

You can view it with followig link:

http://forums.sforce.com/sforce/board/message?board.id=scontrols&message.id=778

It's really weird and i tested it an entire day and i didn't get it to work. You can contact me via my email (johan.buts@pandora.be) if you want the source file.

many thnx
Ron HessRon Hess
lately when i'm seeing strange things from an S-Control or AJAX, I first run it in Firefox, open the Javascript Console ( tools -> javaScript Console

then if there are no error messages there, I use the Venkman Javascript DEbugger, very handy, I can single step thru my code AND thru the AJAX toolkit if i'm feeling brave...

If your not using the Microsoft Debugger or the Venkman debugger on your javascript code, check it out.

http://www.mozilla.org/projects/venkman/
DevAngelDevAngel
It turns out that Johan had a for loop that looked like this:

for (i=0;i}

What was happening was the toolkit had a for loop also that used i and when that bit of toolkit code ran, i ended up being some larger number that qr.records.length.

The solution was to scope the i variable like so:

for (var i=0;i}

Notice the var in the for loop specification. This makes his i "local" to his for loop and not some globally scoped variable that could possible get munched.