Get Latitude and Longitude on Custom Address Fields in Salesforce (US Addresses Only)

This is a fun little way to get Latitude and Longitude for US Addresses on any Object in Salesforce.  Not everyone has the skills or budget to do a Google API callout and get the Latitude and Longitude for any Object, but this is something that can be solved pretty easily for anyone not dealing with foreign addresses.

First, we need to turn on Workflow Rules for the Geocoding from Data.com.  Go to Setup, and type in Data Integration Rules

Data Integration Rules.jpg

Click on the Geocodes for Account Billing Address.  You don’t have to do this on the Account.  You can use Lead or Contact instead.  I would personally suggest that you use whichever of the Objects has the least amount of automation and/or usage volume around it.  If you don’t use Leads at your company, then I would substitute everything “Account” for “Lead”.

Account GeoCode

Select Edit Rule Settings.

Edit Rule Settings.jpg

Uncheck the Bypass workflow rules.  Salesforce is doing an asynchronous callout to Data.com Geo to get the Latitude and Longitude, and that response from Data.com Geo is not going to happen in the transaction of your Save.  It happens quickly after, but it is in a different transaction.  You’ll want to ensure you don’t have any recursive issues that might arise where a Workflow fires twice, so test it thoroughly!

Uncheck bypass WF

Alright, so we’ve got Data.com Geo all set to go.  Now, we need to create a Lookup Field to our Custom Object on the Account.  Make it clear what this field is for, and write a description.  Keep the FLS minimal, as nobody will need to see this field but the Admin.

Lat Long.jpg

We also need to create a Geolocation field, if you haven’t already, on the Project Object.  This will be where you store the Latitude and Longitude values.

Geolocation Field.jpg

Let’s navigate to setup a new Process Builder.

Project PB.jpg

Set Project as the Object this Process Builder runs on.

Set on Project

Now, let’s set our Criteria.  You want to ensure that this fires when you want it to.  I’m accounting for an Address change in my criteria.

Criteria.jpg

Time to setup our Immediate Action of Record Creation (of an Account).

Set Field Values.jpg

Activate and we’re ready to move to the next item.  Creating a Flow that will delete the newly created Account and Update the Project.  Then we’re going to create a new Process Builder to launch a Flow.

The first element of our Flow will be a Record Delete, to Delete the Account.  Drag the Record Delete element out.

DragDelete.jpg

In the Record Delete we’re going to need the AccountId variable that we will pass from our Process Builder to be made as Input Only, allowing that to happen.

AccountId var

Ensure we the Record Delete is setup correctly and hit Save.

DeleteAccount

Now, let’s set our Record Delete as the Start Element of our Flow.

Delete as Start

We need to update our Project, so we will drag out the Record Update element.

Record Update drag

We’ve got a few more variables we need to create.  First, let’s create ProjectId and ensure it is marked as Input Only.

ProjectId

Second, we need to create Latitude and mark it as a Number with a Scale of 8 and Input Only.

Latitude

The last variable we need to create is Longitude and mark it as a Number with a Scale of 8 and Input Only.

Longitude

Let’s fill out the Record Update using these variables.

Record Update

Hit Save and connect the Elements together.

Connect the Dots

Save your Flow.

Save the Flow

Activate your Flow.

Activate Flow

Great, now we’re ready for the last step.  Creating our Process Builder to Launch the Flow.  Navigate back over to create a new Process Builder.

Account PB

Select Account as the Object.

Account PB 2

Now, let’s set our Criteria.  We’re going to ensure that this only fires when the Project field is filled out, so this doesn’t accidentally fire at random.

Account Criteria.jpg

Time to map our Fields inside our Flow Immediate Action.

Flowvars.jpg

Save the Immediate Action, and Activate your Process Builder.

Congrats!  You now have Latitude and Longitude being populated on your Custom Object for all US Addresses.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s