
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Usage of Google Geolocation API
There was a time when ServiceNow had in ist baseline an active code piece to fetch geocoding information (longitude / latitude) from Google for any adress you entered in the location table. This was – due to API restrictions on Google side – deactivated long time ago.
Lately I had the requirement to make this work again. So I hunted and searched for some time on how this used to work – I got it to work for some hours before it stopped again. So I thought why not do a fresh implementation of this using our new technologies like Integration Hub and do it as Low / No-Code piece ? It turned out to be a no-brainer. Here we go :
Google API
First we need to register with Google and create an API Key. For this go to https://developers.google.com/ and create your account. If you have one follow this guide to create an API Key. Once the key is created, restrict it to the API we need. (If the Google Geocode API is not listed, you need to enable it). Here is how it should look like once completed :
That’s it, now we can move to ServiceNow.
Geocode Spoke
Connection & Credential Alias
Open Studio and create a new scoped application, I named mine ‘Google Geocoding Spoke’. Start from scratch – an empty canvas, no template needed.
The first object we need is the Connection & Credential Alias.
Here is mine :
Within that let’s create a connection as follows :
You will need to create a new credential record as API Key Credentials and store your previously generated key there.
With that let’s shift gears and create the most complex part here (don’t worry, still low-code).
Create a new Flow Designer Action
Our Action will need 3 steps, take an address as input and produce some outputs for longitude, latitude and a status code.
Input:
A simple string will do.
Sanitize the input for Google
Google expects us to replace all spaces with ‘+’ signs. A small one-liner script step will do.
Call Google API
REST step connecting to google. Using the Connection & Credential Alias we have limited actions to do here:
- Resource path: /maps/api/geocode/json
- Query Parameters :
- Address Sanitized Address from Script Step
- Key Credential Value
JSON Parser to extract out final return values
The get the sample result from the API you can use a browser and just navigate to an URL like this :
https://maps.googleapis.com/maps/api/geocode/json?address=7900+Creedmoor+Road+Raleigh+NC+27613&key=<...>
What we need are the attributes of location in result.0.geometry. The JSON Parser can easily handle this for us with a config like this.
I have also added the status code so we at least get feedback if all went ok.
That’s it, Action done. You are ready to use this action anywhere in the platform to get longitude / latitude values for your addresses.
Use action on location table
Now that we have the action, let’s put it to use. As an example I am going to use our location table. Create a new flow.
Trigger: Record updated/inserted on table Location
Condition: Any of the relevant fields (street, city, state, zip, country) changes
Actions:
Get Geolocation
Add the new spoke action for getting geolocation. As Address input we use the relevant fields just concatenated with blanks.
Validate result
A simple IF-Flow Logic to make sure we only update the record when we get a good result. This is when status is ‘OK’ and long and lat from our spoke action are populated.
Update the location record
As we have the values now, write them back to our location record.
Summary
As you see, this is really easy and does not require any coding skills. I published this also on my github account in case you want to fork/test it out.
The flow on location table is included, but not activated.
- 2,982 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.