First of all, I am new to Apex. I am trying to create a trigger to do the following: when the user adds or updates an account, the trigger takes the billing address and makes a call to the Google Maps API to automatically get the geolocalization for that address. After getting the geolocalization, the latitude and longitude are added to the database into the custom fields Billing_Latitude__c and Billing_Longitude__c of the account.
To do that, the trigger invokes a public class method, passes to it the address details (street, postal code etc.) so that the Google API call is made, the response JSON is parsed and the latitude and longitude variables are created. Up to this point, I managed to make my code work. But I am having a hard time updating the latitude and longitude custom fields I created.
Here is my trigger:
trigger UpdateGeolocalization on Account (after insert, after update) {
for(Account acct : Trigger.New) {
Geolocalization.generateGeolocalization(acct.BillingStreet, acct.BillingCity, acct.BillingState, acct.BillingPostalCode, acct.BillingCountry, 'billing', acct.Id);
}
}
Here is my class with public method:
public class Geolocalization {
@future (callout=true)
public static void generateGeolocalization(String street, String city, String state, String postalCode, String country, String addressType, Id accountId) {
// Create address string and replace spaces with plus sign
String address = street+' '+city+' '+state+' '+postalCode+' '+country;
String addressWithoutSpaces = address.replaceAll('\\s+',' ');
String addressWithPlusSigns = addressWithoutSpaces.replaceAll(' ','+');
// Call to Maps API
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://maps.googleapis.com/maps/api/geocode/json?address=' + addressWithPlusSigns + '&key=AIzaSyAuxku5rRASGQgKeAOyj38fjsu45847jf8');
request.setMethod('GET');
HttpResponse response = http.send(request);
// Get latitude and Longitude from response JSON
Double lat, lng;
if (response.getStatusCode() == 200) {
JSONParser parser = JSON.createParser(response.getBody());
while (parser.nextToken() != null) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'geometry') {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'location') {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'lat') {
parser.nextToken();
lat = Double.valueOf(parser.getText());
}
if (parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'lng') {
parser.nextToken();
lng = Double.valueOf(parser.getText());
}
}
}
}
}
}
}
// Convert lat and lng to String
String latitudeString = String.valueOf(lat.format());
String longitudeString = String.valueOf(lng.format());
// Code works up till here. If I system.debug the string latitude variable, it is output correctly when I create a new account.
// If the type of account is billing, update the custom geolocalization fields. addressType is a string passed as a method parameter.
if(addressType == 'billing') {
Account acct = [SELECT Name, Billing_Latitude__c FROM Account WHERE Id = :accountId];
//Update latitude custom field
acct.Billing_Latitude__c = latitudeString;
//Update longitude custom field
acct.Billing_Longitude__c = longitudeString;
}
}
}
Any help will be much appreciated!
JSON.deserialize()orJSON.deserializeUntyped()instead of trying to parse the JSON in somewhileloops.