Creating Territories in Salesforce Using Flow

October 12, 2020


Another great use for Salesforce’s Flow Designer is to ensure that the correct data populates on your records at the time that it's supposed to.

In this example, we will use flow and process builder to populate a custom “Territory” field value on the Contact record based on the address entered. Salesforce does have its own Territory Management functions, but these assign territories to accounts, users, and opportunities. Here, we would like a simple way to determine a Contact’s territory. This process can also be replicated for Leads.

List of Territories

First, you must have a list of all countries that Territories will be assigned from. You will need to have a spreadsheet with each country name and the territory associated with that country. This spreadsheet will later be used to insert records.

Custom Object for Country Records

Using just the list of Country names, create a custom “Country” picklist on your Contact object, and post those names in as the values. This custom picklist field provides users with a list of all valid countries that should be available to choose from, allowing them to select a country whose name is properly spelled and formatted.

*Note that this custom field will essentially duplicate the Mailing Country value that can be entered from the standard “Address” field on the Contact record. We handle this by using a workflow to populate the standard “Mailing Country” value (which is part of the standard “Address” field) whenever our custom Country field gets populated or changed. This way, the custom Country field can be used to select the Contact’s country, and the standard Mailing Country will always update to reflect the value chosen. We use this custom field since the standard Mailing Country field cannot be edited to hold the correct country names like the custom picklist can, and this can lead to bad data if country names are not entered properly or have typos in them.

Next, you will need another picklist field on the Contact object to hold each possible Territory value. We have four possible territory values.

The next step involves creating a new custom object to hold your Country records.

Although our object contains a custom field for Country Code and Alpha Country ID, the only necessary fields are the Country Name and the Territory picklist, which should contain all possible territory values (the same values used in the Territory picklist created on the Contact object).

Once your custom object is set up, run a data load to insert the countries and their territories from your spreadsheet.

Once the data load is complete, we then need a validation rule on the Contact object. This is because, although we have both the custom Country picklist that contains all the country names in the correct format and the workflow that populates this value into the standard Mailing Country field, users are still allowed to enter data into the Mailing Country field. This increases the possibility of bad data being entered due to country names not being formatted or spelled correctly. The validation rule uses a VLOOKUP function to match the value entered into the Mailing Country field with the country names of the records inserted into your custom Country object. If the name in the Mailing Country field does not match one of the Country record names exactly, the validation rule triggers and the Contact record cannot be saved.

Finally, the flow to populate the Territory field on the Contact is ready to be created.

Flow Variables

The flow variables we use here are a record variable named StartingRecordID, which will hold the Contact record that gets passed from process builder. And a text variable for the Country Name that is passed from process builder. Also a text variable called CountryTerritory, which is populated from within this flow.

The first element of the flow is a Get Records element. It searches for any of the Country records where the name is equal to the CountryName passed over from process builder. It then stores the Territory value from the Country record it finds into the CountryTerritory flow variable.

The next flow element is an update records element. This element finds the Contact record in question by digging into the StartingRecord record variable, which was passed from process builder to represent the Contact record, and gets its ID. It finds the Contact record with this ID.

It then updates that Contact record’s Territory field with the value in the CountryTerritory flow variable, which was populated in the previous step.

Now you can save and activate the flow.

The final step is to launch this flow from your Contacts process builder.

The criteria node in the flow evaluates to trigger when the Mailing Country field is changed, or when a Contact is created and has a valid Mailing Country value.

Launch the Flow

The action will launch the flow and populate the flow variables with the Contact record and the country value from the Mailing Country field.

Now save and activate the process builder.

Now, whenever a Contact is given a Mailing Country value, whether populated from the workflow triggered by the custom Country field or triggered by entering a valid value into the Mailing Country field, the Territory picklist will update to reflect the Territory for the given country.