Help Using Google Maps Longitude / Latitude Population on Locations and Companies

chaselong
Mega Expert

I'm very interested in using the Mapping Reports in Helsinki, and to do that it looks like I need to populate the longitude / latitude fields for records on my Locations and Companies tables. I read here that there are Business Rules (both named get_lat_long) for these tables that will populate these fields automatically based on the address information on each record. I then read here that you need an API key from Google in order to use this feature now. I've turned on the two business rules, obtained a Google Maps API key, entered it in the "Private key for Google Maps API for Business" under System Properties -> Google Maps, and am still having no luck. When I update location records no lat / long information seems to be pulled in. Could anyone offer me some guidance as to what I'm missing? The problem appears to be on the SN instance side as the Google API dashboard doesn't show any traffic.

1 ACCEPTED SOLUTION

chaselong
Mega Expert

Finally got this to work. Posting the new business rule I ended up using in case anyone else is having an issue with the OOB methods. Disclaimer: I'm not really a coder, I put this together using what I found in the community and the OOB BRs / script includes, so this may not conform to coding best practices.



var street = new String(current.street);


street = street.replaceAll("\r","");


street = street.replaceAll("\n"," ");


street = street.replaceAll(" ","+");



var city = new String(current.city).replaceAll(" ","+");


var state = new String(current.state).replaceAll(" ","+");


var zip = new String(current.zip).replaceAll(" ","+");


var address = "address=" + street + "," + city + "," + state + "," + zip + "&key=YOURGOOGLEAPIKEY";


var ws = new HTTPAdaptor("https://maps.googleapis.com/maps/api/geocode/json?");


var ret = ws.doGet(address);


var lat = 0,


          lng = 0;



try {


  var output = new JSONParser().parse(ret);


  if (output.status == "OK") {


            lat = output['results'][0]['geometry']['location']['lat'];


            lng = output['results'][0]['geometry']['location']['lng'];


            current.latitude = lat;


            current.longitude = lng;


            current.update();


  } else {


            gs.logErr("Get_Lat_Long Lookup Error: " + output.status);


            gs.logErr("Address: " + address);


            gs.logErr("API Response: " + ret);


  }


} catch (err) {


            gs.logErr("Geocoding error: " + jsonOutput);


  }


View solution in original post

10 REPLIES 10

Thanks for taking a look Chase. Below is the BR that I created. It is on a custom table, so the fields are prefixed with "u_" but otherwise I believe that I used the script as you showed it in your previous posts.



I'm wondering too if there are Google Maps properties that need to be changed on a system wide level?



Condition:


current.u_street.changes() || current.u_city.changes() || current.u_state.changes() || current.u_zip.changes() || current.u_country.changes()



Script:


var street = new String(current.u_street);


street = u_street.replaceAll("\r","");


street = u_street.replaceAll("\n"," ");


street = u_street.replaceAll(" ","+");



var city = new String(current.u_city).replaceAll(" ","+");


var state = new String(current.u_state).replaceAll(" ","+");


var zip = new String(current.u_zip).replaceAll(" ","+");


var address = "address=" + u_street + "," + u_city + "," + u_state + "," + u_zip + "&key=AIzaSyAp51V-4UULVhKswij5Uvqzmm7G0-QESwE";


var ws = new HTTPAdaptor("https://maps.googleapis.com/maps/api/geocode/json?");


var ret = ws.doGet(address);


var lat = 0,


          lng = 0;



try {


  var output = new JSONParser().parse(ret);


  if (output.status == "OK") {


          lat = output['results'][0]['geometry']['location']['lat'];


          lng = output['results'][0]['geometry']['location']['lng'];


          current.u_latitude = lat;


          current.u_longitude = lng;


          current.update();


  } else {


          gs.logErr("Get_Lat_Long Lookup Error: " + output.status);


          gs.logErr("Address: " + address);


          gs.logErr("API Response: " + ret);


  }


} catch (err) {


          gs.logErr("Geocoding error: " + jsonOutput);


  }


Try removing the u_ from street, city, state, and zip on the line that begins "var address". Those should be referring to the variables you set in the script earlier, not the custom fields on your table. See if that helps.


Did that help?


Unfortunately just getting back to this and no luck with updating the fields for lat/long


Daniel Draes
ServiceNow Employee
ServiceNow Employee

Just had the same question for a demo and stumbled across a view posts on the community on this with no real answer yet on why/how the baseline feature should work. Some posts  even state they are outdated and do not work at all.

 

I can confirm otherwise 🙂

 

Here is how it is supposed to work:

1) Business Rules get_long_lat triggers on inserts and updates on location and company table, this BR though will only collect the information required for the google api call and store in it sys_geocoding_request.

2) A scheduled job (Process Geocoding Request) is defined that will pick up new requests every 10 seconds and request the data from Google

 

You need to activate the Business Rule as well as the Scheduled Job. I did not need an API key....