Arun Kumar
Custom Sales Territory Management
2022 / Salesforce Project


Role:
Salesforce Developer
Date:
Summer 2022
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 Code | Sales Representative |
91000 | Pawan |
91001 | Pawan |
91002 | Pawan |
91002 | Priyanka |
91010 | Priyanka |
91020 | Priyanka |
92010 | Ram |
92020 | Sunil |
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 Name | Field Type | Description |
Zip Code | Text | The standard name field. Each record will be named after its specific zip code. |
Owner | Lookup (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 Name | Field Type | Description |
Previous Owner | Lookup (User) | The prior sales rep assigned to this Account |
New Owner | Lookup (User) | The new sales rep assigned to this Account |
Previous Territory | Lookup (Territory__c) | The matching Territory__c record for the prior zip code |
New Territory | Lookup (Territory__c) | The matching Territory__c record for the new zip code |
Account | Master-Detail (Account) | The related Account record |
Changed By | Lookup (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

i need Territory Trigger code
cloud please send me bro
LikeLiked by 1 person
Hello Venkat,
All the code is available in GitHub repo : https://github.com/arun12209/Custom-Sales-Territory-Management/tree/master/force-app/main/default/triggers
LikeLike
Can i have the explained video of this project?
LikeLiked by 1 person
OK, sure. I’ll post an explanation video for this project on our YouTube channel. Subscribe to our channel to be notified.
> https://www.youtube.com/channel/UCijMCZnIogcxtrCXPcmOOOQ
LikeLike
Hai Arun
I have subscribed already
Waiting for your video
Thank you.
LikeLike
Hello Naveen,
Here is the project video: https://youtu.be/6nPQ6qhwCHI
Thanks
LikeLike
Is it possible to restrict only one territory against one account..
Also one territory can have only one sales rep assigned…
LikeLiked by 1 person
Hi Mohsin,
Create a relationship b/w Account and Territory and then add the validation that only one territory should be created with an Account.
You can restrict territory creation with duplicate postal code (territory)
LikeLike