You need to sign in to do that
Don't have an account?

passing a list from apex controller to javascript..
Hi everyone i have a list in my Apex class which has soql query result..
String query = 'SELECT City,Latitude__c FROM Lead WHERE City LIKE \''+city+'%\'';
leadrecords = Database.query(query);
Eg :in the above line leadrecords is alist of lead type
private List<Lead> leadrecords;
So now iam trying to pass this list to ajavascript variable so dat javascript variable should have query output...but when i have done like this iam able to pass the list but when iam tryong to display that it is displaying the ids but i want to display the field city and latitude which are storesd in that list how to do it please someone help me with the solution... to javascript variable so how to pass this list to javascript variable
Hello I was able to solve this issue, here is what you should be doing:
1) Create the required list in the Apex Controller
2) Create a Array in the JavaScript on the Page
3) Use the repeat tag to populate this javascript.
4) Use the populated list in any place in the JavaScript.
Here is the sample code::
VF Page
___________________________________________________________________________________________
<apex:page standardController="Lead" extensions="LeadPrivacyRequestCreator">
<script>
var requests = [];
</script>
<apex:repeat value="{!privacyRequests}" var="request">
<script>
requests.push('{!request}');
</script>
</apex:repeat>
<script>
window.onload = alertRequest;
function alertRequest(){
alert(requests[0]);
}
</script>
</apex:page>
________________________________________________________________________________________
Thanks,
Girish.
All Answers
Sounds like you have the string representation of a list there, which is the ids, surrounded by square brackets if I recall correctly.
There's a few ways around this - here's a couple off the top of my head:
You could produce a string representation of the list containing all the fields that you require and parse this via Javascript. Or you could use a repeat tag to iterate the list inside your javascript block and have the body of the repeat contain your javascript statements that populate your array.
Any of this options works for you?
If one of them works for you mark this question as solved or let me know if you need some help.
Set the solution could help to other users with the same doubt find a quick solution :)
Thanks.
leadrecords[i].Latitude__c
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Hi ,
Thank u for your reply, i have tried to loop the list and pass the values like this but iam getting error . So please give me an example of looping and passing that field values to the list and assigning that list to my javascript variable in my visualforce page ......
I have looped as below...
List<Lead> leadsnew = [SELECT City,Latitude__c FROM Lead WHERE City LIKE :(city+'%')];
for(Lead l : leadsnew )
{
leadcity=leadrecords[i].City ;
}
Where leadcity is a list of string datatype
List<String> leadcity= new List<String>();
returning leadcity
public String[] getLeadcity () {
return leadcity;
}
Now i want to catch this value of the list leadcity (city) in a ajavascript variable
So iam doing as follows...
alert('{!leadrecords}');
or
var array1=new Array();
array1='{!leadrecords}';
alert(array1);
But i cant catch the value iam getting null value in alert or sometimes iam getting ids so plz correct my code if i going wrong so that i can catch the value in my javascript
Thanking you,
Your city names are available from the leadcity controller property, but your javascript is still using the leadrecords property.
You are also still passing an array back to the page, so when this is used it will generate a string representation of the entire array, so you will need to parse that string into an array.
Hi bob,
Thanks for your reply iam facing with a small issue will u please help me to correct it actually when iam looping through the list as below and trying to fetch the values of various records of the list.. and trying to assign the values to a string type variable .So as iam asssigning the value to a string type variable as shown below.....
String query = 'SELECT City,Latitude__c,Longitude__c FROM Lead WHERE City LIKE \''+city+'%\'';
leadrecords = Database.query(query);
for(Integer i=1;i<leadrecords.size();i++)
{
leadrecords2= leadrecords[i].Latitude__c;(this is a list of string type if i do this iam getting error like Compile Error: Illegal assignment from String to LIST:String at line 122 column 12 )
longt=leadrecords[i].Longitude__c;
if (i<leadrecords.size())
{
Integer j=i++;
lat1=leadrecords[j].Latitude__c;/*(here lat1 and longt are strings but iam getting only 1st records value but not able to fetch the rest...)
}
}
iam able to fetch only 1st record..... but not the rest of the records So if iam trying to assign like this to a list its giving me an error so please help me how could i achieve itof passing different records of a list to some variable and catching those value in my vpage javascript variables help me with an example or please corect my code if am going wrong....
Hi,
If javaScript is not mandatory you can try this
public ApexPages.StandardSetController leadrecords{
get {
if(leadrecords== null) {
leadrecords= new ApexPages.StandardSetController(Database.getQueryLocator(['SELECT City,Latitude__c,Longitude__c FROM Lead WHERE City LIKE \''+city+'%\'']));
}
return leadrecords;
}
set;
}
public List<Opportunity> getLeadRecords1() {
return (List<Lead>) leadrecords.getRecords();
}
---------------------------------------------------------------
VisualForce Page:
----------------------------------------------------------------
<apex:page controller="ControllerName">
<apex:pageBlock >
<apex:pageBlockTable value="{!LeadRecords1}" var="o">
<apex:column value="{!o.City}"/>
<apex:column value="{!o.Latitude__c}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
------------------------------------------------------------------------------
Please let me know whether above code is helpful. If you find any other solution please share with us.
Hi I thank u for your reply,But my requirement is not to display the values but instead I want to pass the value of Latitude__c field of the retrieved records to the javascript since i want to pass this value to the googlemap in my visualforce page.So that is y iam trying to catch those values .I couldcatch only the first records Latitude__c but not the rest ...SO please help me what could i do to achieve it ...
Here my code:
//i have declared a list of string Datatype//
List<String> leadrecords2= new List<String>();
String query = 'SELECT City,Latitude__c,Longitude__c FROM Lead WHERE City LIKE \''+city+'%\'';
//Iam passing the query output to that list of string type...//
leadrecords = Database.query(query);
Now Looping through the list to fetch each records value
/*for (Lead a : leadrecords ) */
for(Integer i=1;i<leadrecords.size();i++)
{
//Passing those value to the string variable lat and longt
lat= leadrecords[i].Latitude__c;
longt=leadrecords[i].Longitude__c;
}
Now when i pass this list to the visualforce page and trying to assign it to a variable then w3hen i pass this variable to alert box i could see only the first records latitude value but i want to catch the rest so dat i can pass all this variables to my google map (i want to pass this var as a argument to the function which initializes and marks place in googlemap)
SO suggest me in achieving this by giving me an example code in writing for loop and passing those values to d variables and catching them in javascript.
Thank u...
Did anyone solve this challenge?
I have the same need as Anu. In my case, I am building a VF Sites application so I can't use the AJAX toolkit to query my data. I have to get the recordset from the Apex Controller class. Easy enough until I want (have to) to interact with that recordset using javascript which I have to do to implement the Google Maps javascript API.
If someone has working code showing how to iterate through an Apex generated List Object in a VisualForce page using javascript to pull out the individual records, please post.
Thanks in advance.
Hello I was able to solve this issue, here is what you should be doing:
1) Create the required list in the Apex Controller
2) Create a Array in the JavaScript on the Page
3) Use the repeat tag to populate this javascript.
4) Use the populated list in any place in the JavaScript.
Here is the sample code::
VF Page
___________________________________________________________________________________________
<apex:page standardController="Lead" extensions="LeadPrivacyRequestCreator">
<script>
var requests = [];
</script>
<apex:repeat value="{!privacyRequests}" var="request">
<script>
requests.push('{!request}');
</script>
</apex:repeat>
<script>
window.onload = alertRequest;
function alertRequest(){
alert(requests[0]);
}
</script>
</apex:page>
________________________________________________________________________________________
Thanks,
Girish.
Thanks Girish. I did eventually figure this out and can confirm that it works. I was able to create a two dimensional array in javascript and then use the Apex Repeat tag to populate the array with a complete recordset from the Controller. I will post the code when I have some time.
<script>
var requests = [];
</script>
<script>
requests.push('{!wrapper.acc.Qty_Out__c}');
</script>
<script>
window.onload = alertRequest;
function alertRequest(){
var amt=0;
for(int i=0;i<requests.len;i++){
var amt=amt+requests[i];
}
alert(amt);
}
</script>
i would like to get the amount after calculating from list of records in javascript.
Hi,
I am getting single value in alert box from this code. I want to display the list of records in visual force page through the java script code. Please help me out.
Thanks
You have to iterate the list in the page and add each element to the javascript list.
Hi,
I have following problem somewhat si,ilar and looking for solutions. SF gurus, please help.
I have a controller which makes HTTP callouts to a J2EE webapp, the callout returns a list of JSON objects.
I need to retrive values from this object and insert it into salesforce object, there is no VF page for this. I get the following list in response to my call.
var myVal = new Array();
function refreshfilterfieldmap(){
<apex:repeat value="{!map_types}" var="key">
<apex:repeat value="{!map_types[key]}" var="map">
myVal['{!key}'] = '{!map}';
</apex:repeat>
</apex:repeat>
}
can anyone help me how loop map in javascript function for all values of map.