
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-04-2014 07:21 AM
I am trying to use a REST message to return JSON data (using the script below). When I look at my logs I can see that the lat / long values are undefined.
I've tried putting my query with test data (https://maps.googleapis.com/maps/api/geocode/json?address=60%20Cleveland%20street,%20Belfast,%20Unit...) straight into a browser and I get the required results.
Can anyone shed some light as to why I get undefined returns?
var loc = new GlideRecord('cmn_location'); loc.addEncodedQuery("country=United Kingdom^parent=NULL"); // Issue the query to the database to get all records loc.query(); var j = 1; while (loc.next()) { gs.log('Record Found ' + loc.name, 'DC Log'); if (updateLatLong(loc)) { gs.log("Updated: " + loc.name, 'DC Log'); } else { gs.log("Failed to update: " + loc.name, 'DC Log'); } //only process 100 records if (j < 100) j++; else break; } function updateLatLong(loc) { // Create an address from the available fields var address = String(loc.name); var city = String(address.split(" - ")[1]); city = String(city.split(", ")[0]); address = String(address.split(", ")[2]); gs.log('Address = ' + address, 'DC Log'); if (address != undefined && city != undefined) { address += (', ' + city + ', ' + loc.country); gs.log("requesting address:" + address, 'DC Log'); //get the Rest Message service var r = new RESTMessage('Google_Geolocate', 'get'); r.setStringParameter('address', String(address)); var response = r.execute(); // put in a a wait or it will return undefined var k = 1; while (response == null) { response = r.getResponse(1000); k++; if (k > 30) { gs.log('service time-out', 'DC Log'); break; } } gs.log("response took ... " + k + " seconds"); //parse response var parser = new JSONParser(); var parsed = parser.parse(response.getBody()); if (parsed != null && typeof(parsed) != undefined) { if(String(parsed.results[0].geometry.location.lat != undefined)) { loc.latitude = String(parsed.results[0].geometry.location.lat); loc.longitude = String(parsed.results[0].geometry.location.lng); gs.log("Lat/Long = " + String(loc.latitude) + ', ' + String(loc.longitude), 'DC Log'); gs.log('Parsed = ' + String(parsed.results[0].geometry.location.lat), 'DC Log'); var g_address = parsed.results[0].address_components; for (var m = 0; m < g_address.length; m++) { for (var n = 0; n < g_address[m].types.length; n++) { if (g_address[m].types[n] == "country") { loc.country = g_address[m].long_name; } } } } } else { gs.log("Problem with returned JSON", 'DC Log'); } //update record on change if (loc.latitude.changes() || loc.longitude.changes()) { loc.update(); return true; } else { loc.lat_long_error = "true"; loc.update(); return false; } } }
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-08-2014 01:10 AM
I've found the problem if any one is interested...
The script above is fine the problem lays with the google api key.
As I was using the out of the box key the limit on calls had been reached.
I am investigating other free options, as it'll be used once maybe twice.
Has anyone used Bing or another service for Geocoding in SNow?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2014 01:15 AM
Is this the best way to parse the returned JSON?
var parser = new JSONParser();
var parsed = parser.parse(response.getBody());

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-08-2014 01:10 AM
I've found the problem if any one is interested...
The script above is fine the problem lays with the google api key.
As I was using the out of the box key the limit on calls had been reached.
I am investigating other free options, as it'll be used once maybe twice.
Has anyone used Bing or another service for Geocoding in SNow?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-11-2015 10:14 AM
While looking for an answer to my REST problem, where I couldn't get a response through a script, I stumbled upon your source code and the note to "// put in a wait or it will return undefined". Thanks for sharing that bit of wisdom, as I didn't see it in the wiki articles. I also tried using asynchronous REST calls and got nowhere, but your loop waiting for the response variable to populate worked like a charm. I am seeing 8 to 14 loops before I get a response back from the MID server, which makes sense considering that they check in every 15 seconds or so (I think I remember someone at K-15 saying that).

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2015 01:01 AM
Glad it helped
I've noticed a few places in SNow where you need to wait.
Another example I find frequently is in workflows where you update variables then need to wait for a moment for them to update on the server.