I have a trigger that fires when the address has changed on a record.
I make a callout to Google API to gain the longitude and latitude information that I then want to put inside of my record.
When I use database.update(sObject), I get an error that I cannot update inside of an update trigger so I would like to find a work-around for this.
This is the function that will perform the callout:
@future(callout=true) public static void updateGeoLocation( Set<Id> objectsToUpdate, String objectAPI_Name, String longitudeAPI_Name, String latitudeAPI_Name, String streetAPI_Name, String cityAPI_Name, String countryAPI_Name, String zipAPI_Name )
{
System.debug( 'get here?' );
String placeHolder = 'SELECT Id, {0}, {1}, {2}, {3}, {4}, {5} FROM {6}';
List<String> fillers = new List<String>{ longitudeAPI_Name, latitudeAPI_Name, streetAPI_Name, cityAPI_Name, countryAPI_Name, zipAPI_Name, objectAPI_Name };
String soqlQuery = String.format( placeHolder, fillers );
soqlQuery += ' WHERE Id IN : ' + 'objectsToUpdate';
List<sObject> sobjList = Database.query( soqlQuery );
System.Debug( 'number of thingies: ' + sobjList );
for ( sObject sObj : sobjList )
{
String street = ( String )sObj.get( streetAPI_Name );
String city = ( String )sObj.get( cityAPI_Name );
String country = ( String )sObj.get( countryAPI_Name );
String zip = ( String )sObj.get( zipAPI_Name );
String url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + sanitizeLocation( street, city, country, zip ) + '&key=' + Label.GeoApiKeyMassUpdate;
googleMapsResponse res = requestGeoLocation( url );
googleMapsResponse.location loc = res.results[0].geometry.location;
sObj.put( longitudeAPI_Name, loc.lng );
sObj.put( latitudeAPI_Name, loc.lat );
}
update sobjList;
}
Best Answer
Are you calling this in a before update trigger? Because it's not allowed to update the records being processed by the trigger by using a DML operation. Not sure if
@future
has any impact on this.If you are doing it before update, try directly modifying the fields of the objects in
Trigger.new
, otherwise call this function only after update.