You need to sign in to do that
Don't have an account?
Siim
Javascript remoting
Hi there,
I'm not able to make my javascript remoting trial work.
My requirement: I want to save coordinate results i get from geocoding to my account.
I want to use CLIENT SIDE google geocoding to display an inline google map but i also want to save the coordinates.I am trying to use Javacript remoting to save the coordinates. Can i call the apex function directly in $(document).ready(function() as shown below?
My page:
<apex:page standardController="Account" extensions="testJS"> <head> <!-- <apex:includeScript value="{!URLFOR($Resource.BuzzJS, 'buzz_js/jqueryui/js/jquery-1.4.4.min.js')}" />--> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script type="text/javascript"> //var j$ = jQuery.noConflict(); $(document).ready(function() { var myOptions = { zoom: 15, mapTypeId: google.maps.MapTypeId.ROADMAP, mapTypeControl: false } var map; var marker; var geocoder = new google.maps.Geocoder(); var address = "{!Account.BillingStreet}, " + "{!Account.BillingCity}, " + "{!Account.BillingPostalCode}, " + "{!Account.BillingCountry}"; var infowindow = new google.maps.InfoWindow({ content: "<b>{!Account.Name}</b><br>{!Account.BillingStreet}<br>{!Account.BillingCity}, {!Account.BillingPostalCode}<br>{!Account.BillingCountry}" }); geocoder.geocode( { address: address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK && results.length) { if (status != google.maps.GeocoderStatus.ZERO_RESULTS) { //create map map = new google.maps.Map(document.getElementById("map"), myOptions); //center map map.setCenter(results[0].geometry.location); //create marker marker = new google.maps.Marker({ position: results[0].geometry.location, map: map, title: "{!Account.Name}" }); var latitude = results[0].geometry.location.lat(); var longitude = results[0].geometry.location.lng(); alert("Latitude:- "+latitude); alert("Longitude:- "+longitude); alert("{!account.Name}"); testing(latitude,longitude,"{!account}"); //add listeners google.maps.event.addListener(marker, 'click', function() { infowindow.open(map,marker); }); google.maps.event.addListener(infowindow, 'closeclick', function() { map.setCenter(marker.getPosition()); }); } } else { $('#map').css({'height' : '15px'}); $('#map').html("Oops! {!Account.Name}'s billing address could not be found, please make sure the address is correct."); resizeIframe(); } }); function resizeIframe() { var me = window.name; if (me) { var iframes = parent.document.getElementsByName(me); if (iframes && iframes.length == 1) { height = document.body.offsetHeight; iframes[0].style.height = height + "px"; } } } function testing(var lat,var long,var acc) { testJS.setLatLong(lat,long,acc,function(result, event) { // callback function logic if(event.status && event.result) alert("Update successful"); }, {escape:true}) } }); </script> <style> #map { font-family: Arial; font-size:12px; line-height:normal !important; height:250px; background:transparent; } </style> </head> <body> <div id="map"></div> </body> </apex:page>
My controller is as follows:
global class testJS { public final Account acct; // The extension constructor initializes the private member // variable acct by using the getRecord method from the standard // controller. public testJS (ApexPages.StandardController stdController) { this.acct = (Account)stdController.getRecord(); } public testJS(){} @RemoteAction global static void setLatLong ( Decimal lat, Decimal lon,Id accId) { Account acc = [select id, lat__c,lon__c from Account where id =:accid limit 1 ]; acc.lat__c = lat; acc.lon__c = lon; update acc; } }
Am i missing something in implementing the javascript Remoting? Any help would be much appreciated. Many thanks, Siim
Your replies helped me to debug the code better. In fact the error was in the javascript,
My function declaration should have been like
function testing(lat,long,acc) { instead of function testing(var lat,var long,var acc) {
My stupid mistake.Hope his post helps someone.
Thanks you Starz26 for your prompt replies.
Siim
All Answers
Do you have an extension or controller that : {!Account.Name} gets the values from?
Here is a working version of my remoting:
Remote Class:
Java script that calls it:
you can use the {! notation but only if the class (not remote) is able to return that variable, just like you would use it anywhere else on the page outside of the javascript.
Notice that in the remote class, I had to put in a contructor for the calss. All the examples I have seen do not have that but it would not let me save without it....
Hi,
Thanks for your reply.In my case i call my @remote action function directly in the javascript not from the page on load using the
.
However am not sure if this is valid and . To your point i use an extension and have 2 controllers:
I tried to compare other javascript examples i got on the web and yours too.but i dont seem to find any syntax error or something wrong with the way i called my JS function.
The page works but as soon as i add the javascript remoting, the google map is not longer loaded and i suspect its my JS remoting call which is wrong in some way.
Has anybody tried to save the coordinates to salesforce objects using client side geocoding?If yes, what was the approach?
Thanks,
Siim
I too call my remote from the javascript..... (I only posted the exact part that calls it.
Here is my Google maps class.
for my use case, I have row of data, when a row is selected it opens up a detail section and calls the javascript. The Java uses the address of the clicked row to geocode and draw a google map of the address in the detail section....
Also,
The result returned is not true or false in your example it is Null or void. So I believe the reason you are not getting your alert is due to:
I believe you are using the event wrong....
event.status should = True or success or something.... The result object is the results returned from the remote class... the results are not event.results rather it should be result.{FieldName or Value}
Hi again,
You gave a very good exampe using Server side google geocoding. Since i am not sure how salesforce behaves in terms of ip requests when it comes to server side requests and web service? if i have say 250 users and all of them use the the page more than 10 times a day, i would surely exceed the Google API limit which is 2500. That's why i want to use client side geocoding to save the coordinates and reuse it later on.
My goal is to simply save the latitudes and longitudes values i get in the javascript call to the Google Javascript API using
, to the account fields.
I even tried remvoing the condition
before displaying my alert but it does not help.
Is there any other way to save values generated in javascript directly to salesforce object fields in visualforce?
Thanks,
Siim
pass the ID of the account into the remote class and update the account record from there...
Test for a value in the field on the record, if not there, call the geocode class from the remote, get the results, then perform a dml within the remote class to update the fields.
If you want to store information on the records themselves then you will have to do this server side. If you want to store them locally, you will need to create a cookie, and check for that...
Once you are in the remote class you can do dml etc with the passed parameters and not need to use the javascript client side to update the record as you can do it all from within the remote class.
If I am not quite getting your idea of "save the results" please be more specific as to where you want to save them, how they will be reused, etc. Especially when it comes to multiple users at once.
Your replies helped me to debug the code better. In fact the error was in the javascript,
My function declaration should have been like
function testing(lat,long,acc) { instead of function testing(var lat,var long,var acc) {
My stupid mistake.Hope his post helps someone.
Thanks you Starz26 for your prompt replies.
Siim