1

When an account is saved(update or insert) - I want to take its address and then find out the corresponding latitude and longitude from the google maps geocoding api and then save the latitude and longitude into separate fields in the account record. But I am facing the bulkification issue, we are only allowed 10 callouts per transaction, while as per my design the callouts can go upto 200 => Governor Limit error. Is there any way around this, provided I cannot pass information as a bulk to the callout providing service. Please let me know your thoughts. Thanks

1
  • Just fyi, salesforce rolled this out recently, for free, automatic geocoding of all standard address fields via the Google geocode api. Adds lat and long to fields on record. See help.salesforce.com/…
    – gorav
    Jan 29 '16 at 1:40
3

You can use either a Batchable class, Queueable class, or a @future method. At a basic level, a batchable version might look like this:

public class GeoLookupBatch implements Database.Batchable<Account>, Database.AllowsCallouts {
    Account[] records;
    public GeoLookupBatch(Account[] source) {
        records = source;
    }
    public Database.QueryLocator start(Database.BatchableContext context) {
        return Database.getQueryLocator([SELECT Id... FROM Account WHERE Id IN :records]);
    }
    public void execute(Database.BatchableContext context, Account[] scope) {
        // Perform callouts for each record in scope, then update...
    }
    public void finish(Database.BatchableContext context) {
    }
}

The only part left is calling the method when appropriate:

Account[] updatedAddresses = Data.diff(
    Trigger.old, Trigger.new, new SObjectField[] {
        Account.BillingStreet, Account.BillingCity, Account.BillingPostalCode, Account.BillingState, Account.BillingCountry
        });
if(!updatedAddresses.isEmpty()) {
    Database.executeBatch(new GeoLookupBatch(updatedAddresses), 10);
}
2
  • Thank you for the quick reply. If I may ask, will employing these asynchronous methods affect my logic in some way compared to how they would have fared if I were to directly do this from trigger(imagining an utopian scenario of no governor limits). Like will the field be only updated after some time, and hence should I be careful in using logic in the remainder of the trigger - that would depend on the value of these fields that I expected the trigger to populate. Thanks in advance
    – Prajith
    Jan 28 '16 at 19:02
  • @Stygon Usually, "future" methods for small batches (e.g. size = 1) will actually run even before the page gets a chance to reload under normal circumstances, while Queueable and Batchable probably won't. I'd probably build some sort of hybrid system-- use future methods for less than 10 callouts, and Queueable or Batchable for larger values. Regardless, you can guarantee those values will not be available until after the trigger is completed and data has committed to the database.
    – sfdcfox
    Jan 28 '16 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.