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
chiranchiran 

Regarding loopuing the list

Hi everyone iam trying to define a javascript function which displays array values and inturn i pass a list of lead datatype to array..

where leadrecords is a list   
private List<Lead> leadrecords;
and  lead records is returned in this method...


 public PageReference view()
    {
        String query = 'SELECT  City,Latitude__c,Longitude__c FROM Lead WHERE City LIKE \''+city+'%\'';
        
        leadrecords = Database.query(query);
        
        /*for (Lead a : leadrecords ) */
        for(Integer i=1;i<leadrecords.size();i++)
          {
          
           += leadrecords[i].Latitude__c;
           longt=leadrecords[i].Longitude__c;
            if (i<leadrecords.size())
            {
              Integer j=i++;
              lat1=leadrecords[j].Latitude__c;                  
           
          }
          
         
         
       
    }
       return null;
   }


 Now i want to catch  the leadrecords field values (record values) and store it in a variable but not todisplay if i want to display i might have used pageblock table So  the following function trying to catch the values of leads by passing list to array and tring to catch values in for loop of array
function arraycheck()
{
  var array1=new Array();

array1='{!leadrecords}'.replace('[','').replace(']','').split(',');
for(j=0;j<array1.length;j++)
{
   document.write(array1[j] + "<br >");
    var x=array1[j];//giving id of the 1st record 00Q90000001Oj4KEAS//
    var y=array1[j++];//giving id of the 2nd record 00Q90000001Oj6kEAC//
    alert(x);
    alert(y);
}

But i want to catch the field value called Latitude__c from all the records of the list so how to loop the list and catch those values in the variable.....

Please correct me if iam wrong in my code and help me with an example code of doing what iam expecting

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You won't be able to get to the fields on the leads list, as you are accessing a string representation of the list, not the list itself.

 

There's a few ways to do this - here's one:

 

Create a list of strings in addition to leadRecords.

 

Once you have extracted your list of lead records, iterate it and add the Latitude__c value into the list of strings.  

 

Then access the list of strings and break it up in the same way that you are currently breaking up the list of ids.

All Answers

bob_buzzardbob_buzzard

You won't be able to get to the fields on the leads list, as you are accessing a string representation of the list, not the list itself.

 

There's a few ways to do this - here's one:

 

Create a list of strings in addition to leadRecords.

 

Once you have extracted your list of lead records, iterate it and add the Latitude__c value into the list of strings.  

 

Then access the list of strings and break it up in the same way that you are currently breaking up the list of ids.

This was selected as the best answer
chiranchiran

Hi Bob,

 

Nice to see your reply,,i have done as if u suggested now its working great ,but iam seeing a strange behavour here wen i add the list of lead dtype to list of string dtype and passing this list to jscript variable and raised alert iam seeing only few values for example  if  list of lead dtype displays 5 ids of records in the list then the list of string type displaying only 3  field values....

 

Not all the  5 values are passed ...

SO wat could be the issuwe so suggest me how could i resolve this so i can pass all the values to the list ....of string datatype........

chiranchiran

Hi Bob,

 

 

Thank u somuch its working Great!