Let me provide a comprehensive solution covering all aspects of territory hierarchy synchronization:
Hierarchical Data Mapping:
First, build a complete hierarchy map before making any changes. Query all territories and construct a tree structure:
GET /crmRestApi/resources/11.13.18.05/territories?onlyData=true
Parse the response to identify parent-child relationships. Create a map structure:
territoriesMap = {
'TERR_001': {parent: null, children: ['TERR_002', 'TERR_003']},
'TERR_002': {parent: 'TERR_001', children: ['TERR_004']}
}
Parent-Child Validation:
Before applying changes, validate the proposed structure. Check for:
- Circular references: Traverse from each node upward to root, ensuring you never revisit a node
- Orphaned territories: Every territory except root must have a valid parent
- Maximum depth: Oracle CX typically supports 5-7 levels; validate you’re not exceeding limits
Implement validation logic:
function detectCycle(territoryId, visited = new Set()) {
if (visited.has(territoryId)) return true
visited.add(territoryId)
let parent = territoriesMap[territoryId].parent
return parent ? detectCycle(parent, visited) : false
}
Recursive Sync Logic:
Update territories in correct order using a depth-first approach. Process leaf territories first, then work up to root:
- Identify all leaf territories (no children)
- Update their parent references
- Move up one level and repeat
- Continue until root is reached
This ensures parent territories exist before children reference them.
Conflict Detection:
Implement comprehensive conflict checking:
- Duplicate parent assignments: Two territories can’t both claim to be parent of the same child
- Invalid parent references: Parent territory ID must exist in the system
- Assignment conflicts: Sales reps assigned to a territory being deleted must be reassigned
Before executing the sync, run:
POST /crmRestApi/resources/11.13.18.05/territories/validateHierarchy
{
"proposedChanges": [...hierarchy changes...]
}
If validation passes, proceed with updates using a batch approach. Group related changes into a single API call when possible to maintain consistency. If any update fails, immediately roll back previous changes to restore the original hierarchy state.
This approach has successfully handled org restructures involving 500+ territories without sync failures or incorrect assignments.
This draft is based on general Oracle CX Cloud knowledge. It has not been verified against your specific version and environment. Practitioners: verify the steps and share your experience below.