+ Start a Discussion
David Zhu 8David Zhu 8 

Getting "undefined" value in Javascript function

Hi All,
I have a Javascript function to get case geolocation information and put on google map. I follow Salesforce Ajax Kit Dev Guide, but it keeps get "undefined" value when getting the query result. Specifically, in the following lines:

var cid = car.id;
var lat =car.Breakdown_Location__latitude__s;
var lng =car.Breakdown_Location__longitude__s;
alert(cid); //show undefined
alert(lat); //show undefined
alert(lng); //show undefined

I can see local variable "car" has value ""{"type":"Case","Id":"500P0000004ILDvIAO","Breakdown_Location__Latitude__s":"44.8561002","Breakdown_Location__Longitude__s":"-79.9970188"}".

What am I missing?

Thanks,
David
function loadMap() 
    {
    
        var latlng = new google.maps.LatLng(54.351179,-95.502741);
        var myOptions = {
        zoom: 5,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP};
        
        map = new google.maps.Map(document.getElementById("map_canvas"),  myOptions);   
        sforce.connection.sessionId = "{!$Api.Session_ID}";
        var result = sforce.connection.query("select id,Breakdown_Location__latitude__s,Breakdown_Location__longitude__s from case where createddate > 2016-01-10T00:00:00Z and Breakdown_Location__longitude__s <> null limit 3");
        
        //alert(result);
        
        var records = result.getArray("records");
       

        for (var i = 0;i<records.length;i++) 
        {
            //alert(records[i]);

            //debugger;
            
            var car = records[i];   
            //alert(car);
            //debugger;
            var cid = car.id;
            var lat =car.Breakdown_Location__latitude__s;
	        var lng =car.Breakdown_Location__longitude__s;
            //debugger;
			   
           alert(cid); //show undefined
	   alert(lat);   //show undefined
	   alert(lng);  //show undefined
            
            
            marker = new google.maps.Marker({  
            position: new google.maps.LatLng(lat,lng),  
                map: map});      
        }

​

 
Best Answer chosen by David Zhu 8
James LoghryJames Loghry
Be very careful when converting SOQL records into Javascript.  Javascript is case sensitive, where Apex and Visualforce for the most part is the opposite.  For instance, line 28 in your example should be car.Id instead of car.id.  Same is true for Breakdown_Location__Latitude__s and Breakdown_Location__Longitude__s instead of Breakdown_Location__latitude__s and Breakdown_Location__longitude__s

All Answers

James LoghryJames Loghry
Be very careful when converting SOQL records into Javascript.  Javascript is case sensitive, where Apex and Visualforce for the most part is the opposite.  For instance, line 28 in your example should be car.Id instead of car.id.  Same is true for Breakdown_Location__Latitude__s and Breakdown_Location__Longitude__s instead of Breakdown_Location__latitude__s and Breakdown_Location__longitude__s
This was selected as the best answer
David Zhu 8David Zhu 8
Thanks James.