+ Start a Discussion
shan876shan876 

Javascript help plz...

Hi:
  I have two queries which I placed into two arrays, then I loop through the records if the first element in the array is the same as the second array then it will display the record ... for some reason it is not working the way I want it to actually its not working at all....Can someone tell me where I am going wrong.....Please thanks
Code:
<html>
<head>
<script type="text/javascript" src="/soap/ajax/10.0/connection.js"></script>
<script type="text/javascript">
window.onload=init_page;
function init_page() 
{
 if ("{!$User.UserRoleId}"=="00E50000000w27b") 
{
 var j = "";

 var strSQL = "Select  OwnerId,AS400_Account_Number__c,Name,BillingCity,SF_DATE_ON_SERVICE__c from Account where SF_DATE_ON_SERVICE__c = LAST_N_DAYS:30 ORDER BY
SF_DATE_ON_SERVICE__c DESC";

var strSQLB = "Select Id,Name from User where UserRoleId In 
('00E50000000w3yvEAA','00E50000000w2ESEAY','00E50000000w5UrEAI',
'00E50000000wA08EAE','00E50000000w2BEEAY','00E50000000w29rEAA',
'00E50000000w2B4EAI','00E50000000w2BJEAY')";

 var result = sforce.connection.query(strSQL);

var resultB = sforce.connection.query(strSQLB);

 var records = result.getArray("records");

var recordsB = resultB.getArray("recordsB");

if (recordsB[0]==records[0])
{
 for (var i=0; i<records.length; i++) 
{
for (var z=0;z<recordsB.length;z++)
{
  j += "<a href=\"" + records[i].Id + "\" target=\"_blank\">("+(i+1)+") 
"+ recordsB[z].Name +" - "+ records[i].SF_DATE_ON_SERVICE__c+" - "+records[i].AS400_Account_Number__c+" - "+records[i].Name+" - "+records[i].BillingCity+"</a><br>&nbsp;";
 }
}
 document.getElementById("div_tag").innerHTML = j;
} 
}
}
</script>
</head>
<body bgcolor="#F3F3EC">
<font size="2" face="Verdana">
<div id="div_tag">No Accts</div></font>
</body>
</html>

 
michaelforcemichaelforce
I noticed one mistake... the line:
 
var recordsB = resultB.getArray("recordsB");
 
should just be:
 
var recordsB = resultB.getArray("records");
 
The array name is always "records" no matter what you are calling it locally.
razzazrazzaz
Thank You ... But I am still having issues... I do not think its going through the records... I dunno anyone got any suggestions plz
cheenathcheenath
Waht is this check for?

if (recordsB[0]==records[0])
I think this check will always be false.



CaptainObviousCaptainObvious
As cheenath mentioned, if (recordsB[0]==records[0]) will not yield results!
See if the following helps:

Code:
<html>
<head>
<script type="text/javascript" src="/soap/ajax/10.0/connection.js"></script>
<script type="text/javascript">
window.onload=init_page;

//Include this prototype for use in Internet Explorer:
Array.prototype.indexOf = function(searchElement,fromIndex){
 var i = (fromIndex < 0) — this.length+fromIndex : fromIndex || 0;
 for(;i<this.length;i++)
  if(searchElement === this[i]) return i;
 return -1
}

function init_page() {

 if ("{!$User.UserRoleId}"=="00E50000000w27b") {

  var j = "";
  var strSQL = "Select OwnerId,AS400_Account_Number__c,Name,BillingCity,SF_DATE_ON_SERVICE__c " + 
      "From Account " + 
      "Where SF_DATE_ON_SERVICE__c = LAST_N_DAYS:30 " + 
      "ORDER BY SF_DATE_ON_SERVICE__c DESC";

  var strSQLB = "Select Id,Name " +
       "From User " +
       "Where UserRoleId In ('00E50000000w3yvEAA','00E50000000w2ESEAY','00E50000000w5UrEAI'," +
         "'00E50000000wA08EAE','00E50000000w2BEEAY','00E50000000w29rEAA'," +
       "'00E50000000w2B4EAI','00E50000000w2BJEAY')";

  var result = sforce.connection.query(strSQL);
  var resultB = sforce.connection.query(strSQLB);
  var records = result.getArray("records");
  var recordsB = resultB.getArray("records");

  for (var i=0; i<records.length; i++) {
   
   //First, get the owner Id from the first query:
   var ownerID = records[i].OwnerId;
   
   //Then, see if that id exists in the second query:
   var userIndex = recordsB.indexOf(ownerID); 
   
   //If a match is found, the above returns the index in the recordsB query
   //If there is no match, the index will be "-1"
   
   if (userIndex > -1) {   
      j += "<a href=\"" + records[i].Id + "\" target=\"_blank\">("+(i+1)+") " + 
        recordsB[userIndex].Name + " - " + records[i].SF_DATE_ON_SERVICE__c + " - " +
     records[i].AS400_Account_Number__c + " - " + records[i].Name + " - " +
     records[i].BillingCity+"</a><br>&nbsp;";
   }

  }
   document.getElementById("div_tag").innerHTML = j;
 }
}
</script>
</head>
<body bgcolor="#F3F3EC">
<font size="2" face="Verdana">
<div id="div_tag">No Accts</div></font>
</body>
</html>