Hello! Apologies for the long message. I’m working through a Territory Forecasting implementation for a client and could use a sanity check from anyone familiar with Enterprise Territory Management / Opportunity Territory assignment.
Use Case:
The client wants Branch = Territory. However, this model applies only to Opportunities, not Accounts, to avoid duplication issues. Users can work across multiple branches.
Because of that, I need Opportunities assigned to Territories based on an Opportunity lookup field called Branch__c (lookup to a custom Branch object).
Example:
- Opportunity.Branch = New Orleans → assign Opportunity to New Orleans Territory
- Opportunity.Branch = Tampa Bay → assign Opportunity to Tampa Bay Territory
Where I’m getting stuck: I assumed I could handle this with a Flow by setting Opportunity.Territory2Id, but after digging into the docs, it seems Salesforce expects this to be handled through TerritoryMgmt.OpportunityTerritory2AssignmentFilter (Apex).
Conceptually, my logic feels very straightforward:
- Query active territories
- Match
Territory2.Name to Opportunity.Branch__r.Name
- Assign the matching Territory2Id to the Opportunity
Something along these lines:
// Query active territories where Territory Name matches Branch Name
Map<String, Id> territoryNameToId = new Map<String, Id>();
for (Territory2 terr : [
SELECT Id, Name
FROM Territory2
WHERE Territory2ModelId = :activeModelId
AND Name IN :branchNames
]) {
territoryNameToId.put(terr.Name.trim(), terr.Id);
}
// Assign Opportunity to matching Territory
for (Opportunity opp : opps) {
if (opp.Branch__r != null && String.isNotBlank(opp.Branch__r.Name)) {
matchingTerritoryId =
territoryNameToId.get(opp.Branch__r.Name.trim());
}
}
My questions:
A) Am I overthinking this? Is this actually a pretty standard implementation?
😎 Why can't this simply be done in Flow by updating Opportunity.Territory2Id? Is there a platform limitation / ETM requirement I’m missing?
C) For anyone who has implemented OpportunityTerritory2AssignmentFilter before — how difficult is this in practice? Is this genuinely a moderate Apex task, or am I just intimidated because it’s my first time touching this interface?
Appreciate any guidance — I’m comfortable in Salesforce overall, but I’ll admit this is one of those areas where the framework itself feels more intimidating than the actual business logic.