0

This trigger seems to work pretty well for Geocoding accounts...

 trigger AccountGeocode on Account (after insert, after update)
    {
    system.debug('AccountGeocode trigger fired');

    if(System.isBatch() || System.isFuture()) {
        system.debug('Cannot call future methods when this trigger is executed by a future or batch method');
    }
    else {
        for(Account a : Trigger.new) {
            //Will only geocode records while we still have available future calls, so some records may not be geocoded
            if(Limits.getFutureCalls() >= Limits.getLimitFutureCalls()) {
                break;
            }

            if(Trigger.isInsert) {
                    //Call a future method that tries to populate the Geolocation field
                    AccountGeocodeCallout.getLocation(a.id);
              }
              else if(Trigger.isUpdate) {
                    //Call a future method that tries to populate the Geolocation field (if and only if an address field has been changed)
                    Account oldAccount = Trigger.oldMap.get(a.Id);

                    if(oldAccount.ShippingStreet != a.ShippingStreet || 
                        oldAccount.ShippingCity != a.ShippingCity ||
                        oldAccount.ShippingState != a.ShippingState ||
                        oldAccount.ShippingPostalCode != a.ShippingPostalCode ||
                        oldAccount.ShippingCountry != a.ShippingCountry)
                        {
                            AccountGeocodeCallout.getLocation(a.id);
                        }
             }
        }
    } 
}

however I'm still getting emails like the following on bulk updates and confused by how this is happening considering that I'm checking the limits in the trigger.

Apex script unhandled trigger exception by user/organization:

AccountTrigger: System.LimitException: Too many future calls: 11
1
  • 2
    Brad -- you might want to check out Dan Appleman's brilliant async pattern at Dreamforce 13 -- youtube.com/watch?v=ZqqiIg_mynY it allows for unlimited async events (e.g. 'future') through a clever use of scheduled apex-batch apex-customobj.
    – cropredy
    Jun 27, 2014 at 18:01

1 Answer 1

1

I just noticed that the trigger that is throwing the exception is not the same trigger that is checking the limits. facepalm

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .