I'm trying to bulk upload 20,000 Locations (Location__c) which are cities with additional information such as state, country, lat, long, etc. When I try to upload them, I get the following error message, "LocationTrigger: System.LimitException: Too many future calls: 51 Status code: 16"
I've pasted the trigger below. Could someone please tell me what I should change to remove this limit? I'm new to Triggers, so all help is appreciated. Thank you so much!
trigger LocationTrigger on Location__c (after insert, after update) {
for (Location__c l : trigger.new)
{
if(trigger.IsAfter)
{
/*
* After Insert - Geocode location address if address fields are not null
*
*/
if(trigger.isInsert)
{
if (l.Primary_Location__Latitude__s == null && l.City__c!=null && l.ZIP_Code__c!=null && l.State__c!=null)
{
LocationCallouts.getLocationGeocode(l.id);
}
}
/*
* After Update - Geocode location address if address fields have changed and are not null
*
*/
if(trigger.isUpdate)
{
Location__c oldloc = Trigger.oldMap.get(l.Id);
if ((oldloc.City__c!=l.City__c || oldloc.ZIP_Code__c!=l.ZIP_Code__c || oldloc.State__c!=l.State__c) && l.City__c!=null && l.ZIP_Code__c!=null && l.State__c!=null)
{
LocationCallouts.getLocationGeocode(l.id);
}
}
}
}
And here is the Class (updated post to include this):
public class LocationCallouts {
//LocationCallouts.getLocationGeocode('a03M0000005SCKE');
@future (callout=true) // future method needed to run callouts from Triggers
static public void getLocationGeocode(id locID){
// gather account info
Location__c l = [SELECT City__c,Country__c,ZIP_Code__c,State__c FROM Location__c WHERE id =: locID];
// create an address string
String address = '';
//if (a.BillingStreet != null)
// address += a.BillingStreet +', ';
if (l.City__c != null)
address += l.City__c +', ';
if (l.State__c != null)
address += l.State__c +' ';
if (l.ZIP_Code__c != null)
address += l.ZIP_Code__c +', ';
if (l.Country__c != null)
address += l.Country__c;
system.debug('address = ' + address);
address = EncodingUtil.urlEncode(address, 'UTF-8');
// build callout
Http h = new Http();
HttpRequest req = new HttpRequest();
String APIKEY = 'AIzaSyDIC7Hww3ArSvrU9u6JasSnZzjr19OPBw4';
req.setEndpoint('https://maps.googleapis.com/maps/api/geocode/json?address='+address+'&key='+APIKEY);
req.setMethod('GET');
req.setTimeout(60000);
try{
// callout
HttpResponse res = h.send(req);
// parse coordinates from response
JSONParser parser = JSON.createParser(res.getBody());
system.debug('res.getBody() = ' + res.getBody());
double lat = null;
double lon = null;
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'location' || Test.IsRunningTest())){
parser.nextToken(); // object start
while (parser.nextToken() != JSONToken.END_OBJECT){
String txt = parser.getText();
parser.nextToken();
if (txt == 'lat')
{
lat = parser.getDoubleValue();
system.debug('lat = ' + lat);
}
else if (txt == 'lng')
{
lon = parser.getDoubleValue();
system.debug('lon = ' + lon);
}
}
}
}
// update coordinates if we get back
if (lat != null){
l.Primary_Location__Latitude__s = lat;
l.Primary_Location__Longitude__s = lon;
system.debug('l.Primary_Location__Latitude__s ' + l.Primary_Location__Latitude__s);
system.debug('l.Primary_Location__Longitude__s ' + l.Primary_Location__Longitude__s);
update l;
}
} catch (Exception e) {
system.debug('Exception = ' + e);
}
}
@future (callout=true) // future method needed to run callouts from Triggers
static public void getProviderGeocode(id proID){
// gather account info
Provider__c p = [SELECT Physical_Address_1__c,Physical_City__c,Physical_State__c, Physical_ZIP_Code__c, Geolocation__c FROM Provider__c WHERE id =: proID];
// create an address string
String address = '';
if (p.Physical_Address_1__c != null)
address += p.Physical_Address_1__c +',';
if (p.Physical_City__c != null)
address += p.Physical_City__c +',';
if (p.Physical_State__c != null)
address += p.Physical_State__c +' ';
if (p.Physical_ZIP_Code__c != null)
address += p.Physical_ZIP_Code__c +',';
/*
if (p.Country__c != null)
address += p.Country__c;
*/
system.debug('address = ' + address);
address = EncodingUtil.urlEncode(address, 'UTF-8');
// build callout
Http h = new Http();
HttpRequest req = new HttpRequest();
String APIKEY = 'AIzaSyDIC7Hww3ArSvrU9u6JasSnZzjr19OPBw4';
req.setEndpoint('https://maps.googleapis.com/maps/api/geocode/json?address='+address+'&key='+APIKEY);
req.setMethod('GET');
req.setTimeout(60000);
try{
// callout
HttpResponse res = h.send(req);
// parse coordinates from response
JSONParser parser = JSON.createParser(res.getBody());
system.debug('res.getBody() = ' + res.getBody());
double lat = null;
double lon = null;
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
(parser.getText() == 'location')){
parser.nextToken(); // object start
while (parser.nextToken() != JSONToken.END_OBJECT){
String txt = parser.getText();
parser.nextToken();
if (txt == 'lat')
{
lat = parser.getDoubleValue();
system.debug('lat = ' + lat);
}
else if (txt == 'lng')
{
lon = parser.getDoubleValue();
system.debug('lon = ' + lon);
}
}
}
}
// update coordinates if we get back
if (lat != null){
p.Geolocation__Latitude__s = lat;
p.Geolocation__Longitude__s = lon;
system.debug('p.Geolocation__Latitude__s ' + p.Geolocation__Latitude__s);
system.debug('p.Geolocation__Longitude__s ' + p.Geolocation__Longitude__s);
update p;
}
} catch (Exception e) {
system.debug('Exception = ' + e);
}
}
}