Tuesday, January 4, 2022

Convert Address into GPS Coordinates using Saleforce

The GPS coordinates are quite useful in all kinds of projects especially when you need to use any map component. You can easily get them from a street address automatically for Account, Contact, Lead, and some field service objects OOTB.

Inspired by this blog, we can do this for any object. I made some changes (using 2 Flows without Process Builder) to achieve the same goal, considering PB will retire soon.

Get Geolocation for your Custom Object in Salesforce

Using Location object as an example, which interestingly OOTB has location field (Geolocation), but nothing related to address (You need to turn on Field Service to see this object).

The first thing is to build a few fields to save the address data: Street, City, PostalCode, Country, and Address, an optional formula one combining all together like

Street__c + ', ' + City__c + ', ' + Country__c

This one is useful to show a nice formatted address on the record detail page instead of separate fields, and can be used in a URL button like 

https://www.google.com/maps/place/{!Location.Address__c}

I use Geocodes for Lead Address to get the GPS data, so I go to untick Bypass workflow rules, and activate it.


Next, I create the first flow Record-triggered after saved on the Location object. That formula address field comes in handy again because we can check it is changed or the prior record is null, so that only necessary Location records will trigger the following actions in the flow. Obviously, you need to check the address data is not null as well.


The next actions will create a temporary lead record used to get the GPS data. Simply assign the required and address fields to it and create. One important thing is to save the Location record id to the description field of the Lead, and it is used in the next step.


The last step is creating another after saved (updated) flow on Lead object. Make sure to filter down the records (temporary one and with GPS already).


Then use this record's GPS to update the location record (Id in the description field), then delete this lead record.


Simple as that, now once you enter an address on Location, Salesforce (technically a user called Data.com Clean) will grab the GPS data for you automatically!


Things to note:

The process can take a few seconds, so you need to wait a little bit before seeing the magic.

It works well with UI data entry, not sure about Dataloader bulk importing.

Of cause, you also need to enter a real address, otherwise, you may end up with some rubbish leads. You can mitigate this issue by scheduling a flow to delete them every day.

No comments:

Post a Comment

Tips for Salesforce Certified Strategy Designer Exam

A little late to the party, but I just passed the latest Strategy Designer exam. Since it's a really new one (about one week old), there...