Step By Step Guide To Create a Territory Assignment Using Flows In Salesforce - Zennewman/Sales_Territories GitHub Wiki

The purpose of this screen flow is to provide a simple and reliable way of defining custom sales territories in Salesforce. This example exists on the Lead object, but it is equally portable to any standard or custom object.

The needed prerequisites for the flow to function are as follows:

  • Custom field on the Lead(or desired object) called Sales Territory
  • Custom object called Country
  • Custom field on the Country object called Sales Territory in addition to the standard Country Name field.
  • Once the country object is created, upload the CSV attached to this project containing a list of countries in the world. Note, the sales territories on the list can be adjusted as needed for the purpose of the business. Please note as well, that this country list should be current as of 2021, but may need revision prior to use as borders change.
  1. Create a new Auto Launch Flow
  2. Create the Lead variable for input which should look like this:
  3. Create a Get Records node to query for countries. This node needs to filter for the country that matches the county recorded in the lead address field.
  4. Add a decision node to determine if a county match was retrieved successfully. In this example, there is a branch for if no matching country was found and the default option assumes that one is found. Additional logic can be added here as needed to account for other contingencies such as secondary country fields that also need to be accounted for.
  5. In this step, add two Assignment nodes to the canvas to correspond to the two options in the decision node that was just added. The assignment corresponding to the Unknown Country option. The second takes the sales territory value from the country record that was retrieved and assigns it to a new sales territory variable.
  1. Add an Update Records node to the canvas. This will update the original record that started the flow by taking the Sales Territory variable that was assigned in the previous step and passing it back to the Sales Territory field on the original record.

The overall the flow should look approximately like this

With the flow completed, test it out using the debugger to confirm that everything works as expected and then activate it.

Since this flow is an autolaunch flow, the next step is the define how it is triggered. A point of clarification around best practices here: Salesforce flows now support trigger flows that fire off directly instead of needing to be triggered by another process. Best practice in Salesforce, however, is to have a single root trigger per object. Eventually, trigger flows will be able to call autolaunch flows, but that feature is not currently available. Instead, this process will be launched by a process on the lead using Process Builder. Doing this, will allow us to organize and determine the order of operations for this and additional automations that may eventually be built on the lead as well. One goal here is to modularize the automation framework as much as possible to make maintenance and updates easier after the initial configuration.

  1. Create a new process in Process builder for your object if one doesn’t already exist.

  2. Set the new process to trigger when a record is created or edited.

  3. Next set the criteria node to filter on the condition that the Sales Territory custom field on the Lead we made at the beginning to null values only. This will enable to flow to run on leads where the sales territory hasn’t been assigned yet, but prevent it from running on records where it’s already been assigned. If there is a case where country information is updated and the territory needs to be reassessed, then this criteria might need to be revisited.

  4. Next, set the immediate action following the decision note to trigger a flow. Select the Territory Assignment flow and add the lead variable as an input into the flow

  5. The process should looks like this if it is a new process, or, if it’s an existing one that is being extended, then it may have additional nodes.

With both the process and flow activated, the territory assignment is up and running! If this is being built in a sandbox instead of production (as is best practice) then the country file will need to be uploaded to production after the custom object has been pushed for the flow to function properly.