Assign Territories to Opportunities

Lightning bolt icon used to indicate that the content is for Lightning Experience

Attention, Trailblazer!

Salesforce has two different desktop user interfaces: Lightning Experience and Salesforce Classic. This module is designed for Lightning Experience.

You can learn about switching between interfaces, enabling Lightning Experience, and more in the Lightning Experience Basics module here on Trailhead.

Learning Objectives

After completing this unit, you’ll be able to:
  • Explain how to assign a territory manually to an opportunity.
  • Explain how filter-based opportunity territory assignment works.
  • Explain how to set up, deploy, and run filter-based opportunity territory assignment.
  • Explain how to manually exclude an opportunity from filter-based territory assignment.

Introduction

Maria Jimenez, Salesforce admin at Ursa Major, has been working with Lance Park from her sales team to set up the Enterprise Territory Management feature. With Enterprise Territory Management, they’ll be able to easily track which reps are assigned to which accounts and opportunities, and allocate resources efficiently.

You may have followed Maria’s progress in the Enterprise Territory Management Basics module. She’s been working hard! She has most everything she needs in place for territory management for Ursa Major’s accounts. But now it’s time for Maria to take things to the next level: associating territories and opportunities.

Maria does some research in Salesforce Help. First, she learns about assigning a territory to an opportunity manually, which sounds pretty easy. Let’s find out what she learned.

Assign a Territory Manually to an Opportunity

You can manually assign a territory to an opportunity on the opportunity record. When you do so, the opportunity is shared with all users assigned to the territory’s parent in the territory model’s hierarchy. We recommend assigning a territory that’s assigned to the opportunity’s account, but if you have full access to the account, you can assign any territory in the active territory model.

Maria wants to assign the Northern California territory to the Dickenson Mobile Generators opportunity. But first things first. She needs to add a Territory field to the opportunity page layout. Here’s how she does it.
  1. In Setup, click the Object Manager tab, then select Opportunity.
  2. Select Page Layouts and then Opportunity (Marketing) Layout.
  3. In the upper pane, drag the Territory field to the Opportunity Information area under Opportunity Detail.
  4. While she’s at it, Maria also drags the field called “Exclude from the territory assignment filter logic” to the Opportunity Detail area. She’ll use this checkbox later, when she’s working with filter-based opportunity territory assignment.
  5. Click Save.
  6. Next Maria defines the territory for the opportunity. From the App Launcher, select Opportunities and then open the Dickenson Mobile Generators opportunity. (If you’re following along in your Trailhead Playground, you might need to change your list view to show all opportunities first.)
  7. Click Edit, and then in the Territory field, select Northern California from the picklist.
  8. Click Save.

    The Dickenson Mobile Generators opportunity with the Northern California territory assigned

Now the Northern California territory is assigned to the Dickenson Mobile Generators opportunity. So far so good. But Lance is wondering if Maria can automate the process so she can assign a territory to lots of opportunities at the same time. She can! That’s where filter-based opportunity territory assignment comes in.

How Does Filter-Based Opportunity Territory Assignment Work?

Filter-based opportunity territory assignment lets you use a job to assign territories to opportunities. We provide code for an Apex class that you can use as is or modify as needed, based on our guidelines.
Tip

Tip

You can find our code in the Apex Developer Guide. We’ve added a link to it in the Resources section. If you use your own code for the Apex class, it must implement the OpportunityTerritory2AssignmentFilter interface.

After you create and deploy the class, you run the job to complete the assignment process. Job options include making assignments within date ranges and assigning territories to open opportunities only. And you can configure your settings to run the job every time an opportunity is created.

Maria’s ready to get started!

Define the Apex Class for Filter-Based Opportunity Territory Assignment

Here are the steps Maria takes to use our sample code for her Apex class.
  1. From Setup, enter Apex Classes in the Quick Find box, then select Apex Classes and click New.

    The Apex Class edit page in Setup

  2. In the text field on the Apex Class tab, paste our sample code. Maria copied the code from our Apex Developer Guide. If you’re following along in your Trailhead Playground, you won’t be able to complete this step, but you can complete it in your own org. If you use your own Apex code, make sure you note the class name so you can use it in the next task.
  3. Click Save.
Next Maria enables filter-based opportunity territory assignment.

Enable Filter-Based Opportunity Territory Assignment

Here are the steps Maria completes to enable filter-based opportunity territory assignment.
  1. From Setup, enter Territory in the Quick Find box, and then select Territory Settings.
  2. Select the checkbox to enable filter-based opportunity territory assignment.
  3. Enter the class name OppTerrAssignDefaultLogicFilter. (If you’re following along in your Trailhead Playground, you won’t be able to complete this step, but you can complete it in your own org.)
  4. Maria wants to run the filter-based opportunity territory assignment job when opportunities are created, so she selects that option.
  5. Click Save.

    The Territory Settings page with filter-based opportunity territory assignment enabled

Maria’s almost there. Next up: assigning territory priority values.

Determine and Manually Assign Territory Priority Values

The sample Apex code we provide uses territory type priority values to assign territories to opportunities. If you’re using our Apex code or creating a class of your own that uses priority values, determine a scalable priority value scheme that works for you. It’s important to let users assigning priorities know the scheme you’ve determined.

The Territory2Type object’s Priority field accepts numeric values only. If you’re using the sample Apex code we provide (for the OppTerrAssignDefaultLogicFilter class), the higher the number, the higher the priority.

You can assign territory type priority via the API by updating the Territory2Type object’s Priority field. Or you can manually assign priority for individual territory types from the territory type record.

Your highest territory type priority might be 500, next might be 400, then 300, and so on, allowing for intervening values (like 350 or 475) if they’re needed later.

Maria wants to make Ursa Major’s Named Accounts territory type a high priority but leave some room for adjustment. She manually assigns a priority of 400 by completing these steps.
  1. Open the Named Accounts territory type.
  2. Click Edit.
  3. Enter 400 in the Priority field.
  4. Click Save.

    The Named Accounts territory type record with its Priority value edited to 400

The Named Accounts territory type now has a priority value of 400. Maria’s almost ready to run the filter to make the territory assignments. But she doesn’t want the Burlington Textiles Weaving Plant Generator opportunity assigned to a territory. What should she do? Let’s find out.

Manually Exclude an Opportunity from Filter-Based Territory Assignment

You can exclude a specific opportunity from filter-based territory assignment via the API or manually on the opportunity record. To do so, you need a checkbox on the opportunity page layout. Remember when Maria added a field called “Exclude from the territory assignment filter logic” to her opportunity page layout? That’s why! Here are the steps she takes now to exclude the Burlington Textiles Weaving Plant Generator opportunity from the territory assignment. (You won’t be able to follow along in your Trailhead Playground, but you can exclude an opportunity if you need to in your own org.)
  1. Open the Burlington Textiles Weaving Plant Generator opportunity.
  2. Click Edit and select Exclude from the territory assignment filter logic. (If you’re following along in your Trailhead Playground, you won’t see Maria’s opportunity details.)

    The opportunity edit page with “Exclude from the territory assignment filter logic” selected

  3. Click Save.

Run the Opportunity Territory Assignment Filter

The opportunity territory assignment filter assigns territories to opportunities based on the filter logic in the Apex class.

Now that Maria has set up the filter and excluded one opportunity, it’s time to make the assignments! Here are the steps she completes to run the filter for open opportunities only.
  1. From Setup, enter Territory Models in the Quick Find box, then select Territory Models.
  2. View the Northern California territory model’s hierarchy.
  3. Click Run Opportunity Filter. (If you’re following along in your Trailhead Playground, you won’t be able to complete this task, but you can complete it in your own org.)
  4. Select Include only open opportunities.

    The opportunity territory filter ready to run for open opportunities only

  5. Click Run Opportunity Filter.
Maria receives an email when the job is complete. Now all the opportunities specified by the filter parameters have a territory assigned, except for Burlington Textiles Weaving Plant Generator, which Maria excluded. Wasn’t that easy?

Now it’s your turn to review what you’ve learned.

retargeting