[SalesForce] Trying to access account geolocation fields in after insert trigger

I'd like to use the Salesforce account Geolocation fields (billing address latitude and longitude) to assign accounts to specific geographical zones.

Salesforce automatically updates the billing address latitude and longitude when the account is created – the problem I'm running into is the slight (second or less) lag time between the creation of the record, and Salesforce's automatic update of the coordinates.

When the coordinates are referenced in a class called by the After Insert trigger, they return null.

The only workarounds I've come up with are – using the Google Maps API (but callout limits may be exceeded), trying a hacky sleep function, or writing a schedulable batch that processes new accounts – but I could see this frustrating users as they wait for the info to be populated on their new accounts.

Hoping there's another option I'm missing here – thanks for any advice.


Just a warning to anyone else who may need to do something similar – got this working thanks to the advice here but initially the update of the Geolocation fields still bypassed the After Update trigger – even with the Bypass Trigger checkbox unchecked. A Process Builder process (that ran on account creation/update) seemed to be the cause. Once that process was deactivated all began to work as expected.

Best Answer

The geocoding data rules are asynchronous and you will not have access to their data in the after insert operation because the geocoding rule is executed after the insert transaction is completely finished.

Because the geolocation data is set in an update, you can use an update trigger instead and use the geolocation data on the records in the trigger.

Also, be mindful that there's an accuracy field set during geocoding which you may need to inspect to ensure you're assigning records based on your specific business needs.

https://help.salesforce.com/articleView?id=data_dot_com_clean_geocode_information_fields.htm

Related Topic