Salesforce Integration with Google Maps
Updated: Jan 17, 2020
To commence with, Integration is calling an external service into our application. Most enterprise-level applications have a need to integrate with other applications used by the same organization. These integration usually cater to different layers, like Data, Business Logic, Presentation and Security, depending on the requirement. This helps organizations achieve greater levels of operational consistency, efficiency and quality.
When we implement anything in Salesforce, we frequently need to integrate it with other applications. In this article we will integrate salesforce with google maps. For this we will use apex callout to tightly integrate with an external service. When we say callout that simply means sending an request to a service and receiving a response. Mainly callouts work in two ways:
1. Web service callouts using SOAP
2. HTTP callouts using REST
We will be doing integration by using HTTP callouts with REST as it is easier to use, requires much less code and it utilize JSON which is easily readable. First thing is, whenever we are making a callout to extenal web service it should be authorized. If it is not, then you will never be able to access that service. Before start anything we will add our approved sites in the remote settings sites page.
We are making calls to following site:
Authorize above endpoint URLs by following these steps :
1. From Setup, enter Remote Site Settings in the Quick Find box, then click Remote Site Settings.
2. Click New Remote Site.For the remote site name and URL.
3. Click Save.
Before making callout to google web services, we need to create authorization key.
1. Goto https://console.developers.google.com
2. Click on top left side of window to create project (Click, blue tick is).
This will open below window, and click on "New Project".
3. Enter Project Information, and Create Project.
4. Click "Enable APIs and Services".
5. Enable this Api. You can also check out it's documentation to see it's endpoint and input output parameters.
6. Click on Credentials, to create API key. This is needed to call Map Api.
Click on "Api Key", this will generate API Key.
7. Copy API Key, and Save it somewhere.
Now we can jump to coding....
Next step is to make an apex callout to google maps API. The following example sends an GET request to web service. And the service sends response in JSON format. So, we will use JSON parser to read JSON format.
Here we need the API key which we have created earlier.
in variable "yourAPiKey", save your api key.
Execute this class from Anonymous Window, and check debug logs, you will see longitude and Latitude in variable 'lat' and 'lon'.
Although I have provided all the steps, but if you fail at any step please log your comment with your query, and I will surely reply.