The CreatorCon Call for Content is officially open! Get started here.

REST JSON response undefined

Daryll Conway
Giga Guru

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;
               }
       }
}
1 ACCEPTED SOLUTION

Daryll Conway
Giga Guru

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?


View solution in original post

8 REPLIES 8

Joe Wilmoth
ServiceNow Employee
ServiceNow Employee

Try using JSUtil.logObject() instead of gs.log and see if that helps.



Thanks,


Joe


I've tried using the JSUtil.logObject() and I get 'null, undefined, or not an object'


I've seen on the wiki that there are REST API debugging tools with Eureka. However I am running Dublin so I'm not sure how to debug this.


Daryll Conway
Giga Guru

Can anyone suggest any reason why this object is only undefined when pulled into Service Now?