You need to sign in to do that
Don't have an account?
Andrew Aldis
Google Geo Code API wont update the record
I wrote a trigger and class to call out to the Google Geo location api and return the latitude and longitude then update a geolocation field on a location record. Everything seems to work with the API I can see from Debug Logs that the class gets the location but it stops short of updating the record. My trigger and class are below.
CLASS
public class FslLocationGeocodeAddress {
// static variable to determine if geocoding has already occurred
private static Boolean geocodingCalled = false;
// wrapper method to prevent calling futuremethods from an existing future context
public static void DoAddressGeocode(id locationId) {
if(geocodingCalled || System.isFuture()) {
System.debug(LoggingLevel.WARN,'***Address Geocoding Future Method Already Called - Aborting...');
return;
}
// if not being called from future context, geocode the address
geocodingCalled = true;
geocodeAddress(locationId);
}
// we need a future method to call Google Geocoding API from Salesforce
@future (callout=true)
static private void geocodeAddress(id locationId)
{
// Key for Google Maps Geocoding API
String geocodingKey = 'AIzaSyDsXhgOWw8h98l2rWnOcRy4j8_l8Evx3Ms';
// get the passed in address
CKSW_BASE__Location__c geoLocation = [SELECT Street_Address__c, City__c, State_Providence__c, Country__c, Zipcode__c FROM CKSW_BASE__Location__c WHERE id = :locationId];
//check that we have enough information to geocode the address
if((geoLocation.Street_Address__c == null) || (geoLocation.City__c == null)) {
System.debug(LoggingLevel.WARN,'Insufficient Data to Geocode Address');
return;
}
//create a string for the address to pass to Google Geocoding API
String geoAddress = '';
if(geoLocation.Street_Address__c != null)
geoAddress += geoLocation.Street_Address__c + ', ';
if(geoLocation.City__c != null)
geoAddress += geoLocation.City__c + ', ';
if(geoLocation.State_Providence__c != null)
geoAddress+= geoLocation.State_Providence__c + ', ';
if(geoLocation.Country__c != null)
geoAddress+= geoLocation.Country__c + ', ';
if(geoLocation.Zipcode__c != null)
geoAddress+= geoLocation.Zipcode__c;
System.debug('GeoAddress is set '+geoAddress);
//encode the string so we can pass it as part of URL
geoAddress= EncodingUtil.urlEncode(geoAddress, 'UTF-8');
//build and make the callout to the Geocoding API
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://maps.googleapis.com/maps/api/geocode/json?address='+geoAddress + '&key=' + geocodingKey + '&sensor=false');
request.setMethod('GET');
request.setTimeout(60000);
try {
//make the http callout
HttpResponse response = http.send(request);
System.debug('The Http response is '+response);
string body = response.getBody();
System.debug('The response body is' + body);
//parse JSON to extract co-ordinates
JSONParser responseParser = JSON.createParser(response.getBody());
system.debug('The response parser is'+responseParser);
//initialize co-ordinates
double latitude = null;
double longitude = null;
system.debug('Next token prior to the while nextToken != Null is '+responseParser.nextToken());
//while loop 1
while(responseParser.nextToken() != null ) {
if((responseParser.getCurrentToken() == JSONToken.FIELD_NAME) && (responseParser.getText() == 'location')) {
system.debug('If condition true');
system.debug('Current Token = '+responseParser.getCurrentToken()+' Current text = '+responseParser.getText());
responseParser.nextToken();
system.debug('Next Token = '+responseParser.nextToken());
while (responseParser.nextToken() != JSONToken.END_OBJECT) {
system.debug('While condition 2 true');
String locationText = responseParser.getText();
latitude = responseParser.getDoubleValue();
system.debug('responseParser.getText() = '+responseParser.getText());
system.debug('responseParser.getDoubleValue() = '+responseParser.getDoubleValue());
latitude = responseParser.getDoubleValue();
system.debug(latitude);
responseParser.nextToken();
system.debug('responseParser.nextToken() = '+responseParser.nextToken());
system.debug('text ='+responseParser.getText());
system.debug('responseParser.getDoubleValue() = '+responseParser.getDoubleValue());
longitude = responseParser.getDoubleValue();
system.debug(longitude);
}
}
}
system.debug('Latitude = '+latitude);
system.debug('Longitude = '+longitude);
if (longitude != null && latitude != null){
system.debug('latitude != null');
geoLocation.Geolocation__Latitude__s = latitude;
system.debug('Latitude is '+geoLocation.Geolocation__Latitude__s);
geoLocation.Geolocation__Longitude__s = longitude;
system.debug('Longitude is '+geoLocation.Geolocation__Longitude__s);
system.debug('geoLocation id '+geoLocation.Geolocation__c);
update Geolocation;
}
} catch(Exception e) {
System.debug(LoggingLevel.ERROR, 'Error Geocoding Address - ' + e.getMessage());
}
}
}
TRIGGER
trigger FslLocationGeocodeAddress on CKSW_BASE__Location__c (after insert, after update) {
//bulkify trigger in case of multiple locations
for(CKSW_BASE__Location__c location : trigger.new) {
//check if Billing Address has been updated
Boolean addressChangedFlag = false;
if(Trigger.isUpdate) {
CKSW_BASE__Location__c oldLocation = Trigger.oldMap.get(location.Id);
if((location.Street_Address__c != oldLocation.Street_Address__c) || (location.City__c != oldLocation.City__c) ||
(location.Country__c != oldLocation.Country__c) || (location.Zipcode__c != oldLocation.Zipcode__c)) {
addressChangedFlag = true;
System.debug(LoggingLevel.DEBUG, '***Address changed for - ' + oldLocation.Name);
}
}
// if address is null or has been changed, geocode it
if((location.Geolocation__Latitude__s == null && location.Geolocation__Longitude__s == null) || (addressChangedFlag == true)) {
System.debug(LoggingLevel.DEBUG,'***Geocoding CKSW_BASE__Location__c - ' + location.Name);
FslLocationGeocodeAddress.DoAddressGeocode(location.id);
}
}
}
CLASS
public class FslLocationGeocodeAddress {
// static variable to determine if geocoding has already occurred
private static Boolean geocodingCalled = false;
// wrapper method to prevent calling futuremethods from an existing future context
public static void DoAddressGeocode(id locationId) {
if(geocodingCalled || System.isFuture()) {
System.debug(LoggingLevel.WARN,'***Address Geocoding Future Method Already Called - Aborting...');
return;
}
// if not being called from future context, geocode the address
geocodingCalled = true;
geocodeAddress(locationId);
}
// we need a future method to call Google Geocoding API from Salesforce
@future (callout=true)
static private void geocodeAddress(id locationId)
{
// Key for Google Maps Geocoding API
String geocodingKey = 'AIzaSyDsXhgOWw8h98l2rWnOcRy4j8_l8Evx3Ms';
// get the passed in address
CKSW_BASE__Location__c geoLocation = [SELECT Street_Address__c, City__c, State_Providence__c, Country__c, Zipcode__c FROM CKSW_BASE__Location__c WHERE id = :locationId];
//check that we have enough information to geocode the address
if((geoLocation.Street_Address__c == null) || (geoLocation.City__c == null)) {
System.debug(LoggingLevel.WARN,'Insufficient Data to Geocode Address');
return;
}
//create a string for the address to pass to Google Geocoding API
String geoAddress = '';
if(geoLocation.Street_Address__c != null)
geoAddress += geoLocation.Street_Address__c + ', ';
if(geoLocation.City__c != null)
geoAddress += geoLocation.City__c + ', ';
if(geoLocation.State_Providence__c != null)
geoAddress+= geoLocation.State_Providence__c + ', ';
if(geoLocation.Country__c != null)
geoAddress+= geoLocation.Country__c + ', ';
if(geoLocation.Zipcode__c != null)
geoAddress+= geoLocation.Zipcode__c;
System.debug('GeoAddress is set '+geoAddress);
//encode the string so we can pass it as part of URL
geoAddress= EncodingUtil.urlEncode(geoAddress, 'UTF-8');
//build and make the callout to the Geocoding API
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://maps.googleapis.com/maps/api/geocode/json?address='+geoAddress + '&key=' + geocodingKey + '&sensor=false');
request.setMethod('GET');
request.setTimeout(60000);
try {
//make the http callout
HttpResponse response = http.send(request);
System.debug('The Http response is '+response);
string body = response.getBody();
System.debug('The response body is' + body);
//parse JSON to extract co-ordinates
JSONParser responseParser = JSON.createParser(response.getBody());
system.debug('The response parser is'+responseParser);
//initialize co-ordinates
double latitude = null;
double longitude = null;
system.debug('Next token prior to the while nextToken != Null is '+responseParser.nextToken());
//while loop 1
while(responseParser.nextToken() != null ) {
if((responseParser.getCurrentToken() == JSONToken.FIELD_NAME) && (responseParser.getText() == 'location')) {
system.debug('If condition true');
system.debug('Current Token = '+responseParser.getCurrentToken()+' Current text = '+responseParser.getText());
responseParser.nextToken();
system.debug('Next Token = '+responseParser.nextToken());
while (responseParser.nextToken() != JSONToken.END_OBJECT) {
system.debug('While condition 2 true');
String locationText = responseParser.getText();
latitude = responseParser.getDoubleValue();
system.debug('responseParser.getText() = '+responseParser.getText());
system.debug('responseParser.getDoubleValue() = '+responseParser.getDoubleValue());
latitude = responseParser.getDoubleValue();
system.debug(latitude);
responseParser.nextToken();
system.debug('responseParser.nextToken() = '+responseParser.nextToken());
system.debug('text ='+responseParser.getText());
system.debug('responseParser.getDoubleValue() = '+responseParser.getDoubleValue());
longitude = responseParser.getDoubleValue();
system.debug(longitude);
}
}
}
system.debug('Latitude = '+latitude);
system.debug('Longitude = '+longitude);
if (longitude != null && latitude != null){
system.debug('latitude != null');
geoLocation.Geolocation__Latitude__s = latitude;
system.debug('Latitude is '+geoLocation.Geolocation__Latitude__s);
geoLocation.Geolocation__Longitude__s = longitude;
system.debug('Longitude is '+geoLocation.Geolocation__Longitude__s);
system.debug('geoLocation id '+geoLocation.Geolocation__c);
update Geolocation;
}
} catch(Exception e) {
System.debug(LoggingLevel.ERROR, 'Error Geocoding Address - ' + e.getMessage());
}
}
}
TRIGGER
trigger FslLocationGeocodeAddress on CKSW_BASE__Location__c (after insert, after update) {
//bulkify trigger in case of multiple locations
for(CKSW_BASE__Location__c location : trigger.new) {
//check if Billing Address has been updated
Boolean addressChangedFlag = false;
if(Trigger.isUpdate) {
CKSW_BASE__Location__c oldLocation = Trigger.oldMap.get(location.Id);
if((location.Street_Address__c != oldLocation.Street_Address__c) || (location.City__c != oldLocation.City__c) ||
(location.Country__c != oldLocation.Country__c) || (location.Zipcode__c != oldLocation.Zipcode__c)) {
addressChangedFlag = true;
System.debug(LoggingLevel.DEBUG, '***Address changed for - ' + oldLocation.Name);
}
}
// if address is null or has been changed, geocode it
if((location.Geolocation__Latitude__s == null && location.Geolocation__Longitude__s == null) || (addressChangedFlag == true)) {
System.debug(LoggingLevel.DEBUG,'***Geocoding CKSW_BASE__Location__c - ' + location.Name);
FslLocationGeocodeAddress.DoAddressGeocode(location.id);
}
}
}