Custom Sales Territory Management

Arun Kumar

2022 / Salesforce Project

Requirements

Green Motors Inc. has seen exponential sales growth since the release of its popular Model X solar-powered sports car.

They have a sales team that sells cars to auto dealers all over India. Each sales representative is responsible for one or more zip codes. Sales representatives are in charge of all auto dealers in their zip code territories.

Green Motors Inc. currently uses Salesforce to track its sales activities, including

  • Accounts: Each account represents an automobile dealer.
  • Opportunities: A sale of vehicles to an automobile dealer.
  • Contacts: Automobile dealer employees.

The Sales Operations team uses a combination of Salesforce and spreadsheets to manage each sales rep’s territory. They manually set the Account Owner field to the sales rep’s User record when the Account’s Zip Code belongs to the sales rep. Currently, a list of zip codes and their sales rep is stored in a spreadsheet that looks like this:

Zip CodeSales Representative
91000Pawan
91001Pawan
91002Pawan
91002Priyanka
91010Priyanka
91020Priyanka
92010Ram
92020Sunil

Since Green Motors Inc.’s sales team has tripled in size in the past year, the Sales Operations team is having trouble keeping up with all the sales territory changes. This process must be automated!

Requirement #1: Recreate the Zip Code spreadsheet as a custom Territory object. The custom object should have the following custom fields:

Field NameField TypeDescription
Zip CodeText The standard name field. Each record will be named after its specific zip code.
OwnerLookup (User)The standard owner field. The sales rep assigned to this territory

Note: All fields should have field history tracking turned on.

Requirement #2: When an Account’s BillingPostalCode (aka Zip Code), is changed, 

  • Change the Account Owner to the sales representative assigned to the new zip code
  • Change the Owner field of all the Account’s Contacts to the same sales rep 
  • Change the Owner field of all the Account’s Open Opportunities to the same sales rep

Note: 

  • The logic should run only when the Account’s zip code is changed or populated for the first time
  • If no matching Territories are found, do nothing 

Requirement #3: Multiple sales representatives can be assigned to the same zip code territory. If this is the case, use a random function to select one of the assigned sales representatives.

Requirement #4: Three sales representatives at most can be assigned to a single zip code. Display an error if a user attempts to associate another sales representative with a zip code.

Requirement #5: Create an Assignment History custom object with the following fields:

Field NameField TypeDescription
Previous OwnerLookup (User)The prior sales rep assigned to this Account
New OwnerLookup (User)The new sales rep assigned to this Account
Previous TerritoryLookup (Territory__c)The matching Territory__c record for the prior zip code
New TerritoryLookup (Territory__c)The matching Territory__c record for the new zip code
AccountMaster-Detail (Account)The related Account record
Changed ByLookup (User)The user who changed the BillingPostalCode

Requirement #6: Create an Assignment_History__c record whenever an Account’s BillingPostalCode is changed or populated for the first time. All fields should be populated.

Requirement #7: If a Territory__c record’s sales representative is changed (and only when it is changed), repeat Requirement #2’s logic for all Accounts associated with the Territory.

Requirement #8: At least 90% test code coverage

Solution

Data Model

Account Trigger

Territory Trigger

Account After Record Triggered Flow

Update Contacts Owner Flow

Update Opportunities Owner Flow

Insert Assignment History Flow

Territory After Record Triggered Flow

GitHub Repository

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 )

Twitter picture

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

Facebook photo

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

Connecting to %s