# How are large-scale territory assignment recalculations scheduled and optimized for massive enterprise account bases?

**Topic:** Sales Cloud · **Difficulty:** Medium · **Tags:** Sales Cloud, Territory Management, Batch Processing

Read it on the site: https://www.forcechallenges.com/interview-questions/how-are-large-scale-territory-assignment-recalculations-scheduled-and-optimized-for-massive-enterprise

## Answer

**Basics:** Enterprise Territory Management (ETM) assigns accounts to territories (for example "EMEA - Banking") using rules on account fields such as Industry, Billing Country or Annual Revenue. Territories form a hierarchy inside a Territory Model, and a model is in one of three states: Planning, Active or Archived. Only one model is active at a time. Opportunities inherit a territory from their account, so when a rule change moves an account, the pipeline moves with it.

**How it works:**
1. Build or clone a model in Planning state, add Territory Types, territories and assignment rules (Setup > Territory Models).
2. Run Rules in the Planning model and inspect which accounts land where, without affecting the live model.
3. Activate the model. In the active model, rules run automatically on account create/edit when the "Evaluate this account against territory rules on save" behavior applies.
4. For bulk recalculation (after changing rules, or after a data load) use Run Rules on a territory or on the whole model. It runs as a background job; it is not an Apex batch you write.
5. Rules apply to accounts; opportunity territory is set by default logic or by an Apex filter.

**Advanced / deep dive:** There is no built-in cron for Run Rules, so teams that need scheduled re-evaluation touch accounts in a scheduled Batch Apex job with the territory-evaluation option on the DML (verify the exact mechanism in current docs), or simply run rules manually in a maintenance window. Heavy runs are the main cause of sharing recalculation load, so avoid running them at quarter-end. Design for fewer, broader rules, prefer indexed/standard fields, and keep territories per model within documented limits (verify current numbers). Manual assignments (via Assign Territories) are not removed by rules, which is how you protect named-account exceptions. Legacy Territory Management 1.0 is not available to new orgs; ETM is the only option.

**Code / config example:** Custom opportunity-to-territory logic (default logic uses the account's territory):
```apex
global class OppTerrFilter implements TerritoryMgmt.OpportunityTerritory2AssignmentFilter {
    global Map<Id, Id> getOpportunityTerritory2Assignments(List<Id> opportunityIds) {
        Map<Id, Id> oppToAcct = new Map<Id, Id>();
        for (Opportunity o : [SELECT Id, AccountId FROM Opportunity WHERE Id IN :opportunityIds]) {
            oppToAcct.put(o.Id, o.AccountId);
        }
        // One query; first row per account wins (highest priority type first)
        Map<Id, Id> acctToTerr = new Map<Id, Id>();
        for (ObjectTerritory2Association a : [
                SELECT ObjectId, Territory2Id FROM ObjectTerritory2Association
                WHERE ObjectId IN :oppToAcct.values()
                ORDER BY Territory2.Territory2Type.Priority ASC]) {
            if (!acctToTerr.containsKey(a.ObjectId)) acctToTerr.put(a.ObjectId, a.Territory2Id);
        }
        Map<Id, Id> result = new Map<Id, Id>();
        for (Id oppId : oppToAcct.keySet()) result.put(oppId, acctToTerr.get(oppToAcct.get(oppId)));
        return result;
    }
}
```

**Hidden gotchas & must-remember points:**
- Only one territory model is active; you can keep several in Planning (check the current cap).
- Run Rules is asynchronous; results are not instant on millions of accounts.
- Rules never remove manually assigned territories.
- An opportunity can sit in only one territory, but an account can be in many; that is why the filter above must pick one.
- Activating a new model changes forecast and sharing structures; test first.
- Territory sharing creates share rows, so large reassignments cause sharing recalculation.

**Best practice:** Validate every rule change in a Planning model, then run rules off-hours and monitor the job.

---
Source: ForceChallenges · last updated 2026-11-02
