function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Benjamin ChismBenjamin Chism 

Can anyone help with territory opportunity assignment filter?

Can anyone see what my problem is? We created an APEX class for the following territoryopportunityfilter.
I want the opportunity to assign to the territory of the owner territory association.

See code:
global class OppTerrAssignDefaultLogicFilter implements TerritoryMgmt.OpportunityTerritory2AssignmentFilter { 

    global OppTerrAssignDefaultLogicFilter() {}

    global Map<Id,Id> getOpportunityTerritory2Assignments(List<Id> opportunityIds) { 
        Map<Id, Id> OppIdTerritoryIdResult = new Map<Id, Id>();
        
        // Get the active territory model Id
        Id activeModelId = getActiveModelId();
        
        if(activeModelId != null){

            List<Opportunity> opportunities =
                [Select Id, OwnerId, Territory2Id from Opportunity where Id IN :opportunityIds];

            Set<Id> ownerIds = new Set<Id>();
            // Create set of parent ownerIds
            for(Opportunity opp:opportunities){
                if(opp.OwnerId != null){
                    ownerIds.add(opp.OwnerId);
                }
            }
           
            Map<Id,Territory2Priority> ownerMaxPriorityTerritory = getownerMaxPriorityTerritory(activeModelId, ownerIds);
            
            // For each opportunity, assign the territory of the opportunity to the value of the highest priority territory of the owner
            for(Opportunity opp: opportunities){
                Territory2Priority tp = ownerMaxPriorityTerritory.get(opp.OwnerId);
                // Assign highest priority territory if there is only 1.
                if((tp != null) && (tp.moreTerritoriesAtPriority == false) && (tp.territory2Id != opp.Territory2Id)){
                    OppIdTerritoryIdResult.put(opp.Id, tp.territory2Id);
                    System.debug('Territory ID ' + tp.territory2Id + 'assigned to opportunity ' + opp.Id);
                }else{
                    OppIdTerritoryIdResult.put(opp.Id, null);
                }
            }
        }
        return OppIdTerritoryIdResult;
    }
    
    private Map<Id,Territory2Priority> getOwnerMaxPriorityTerritory(Id activeModelId, Set<Id> ownerIds){

        Map<Id,Territory2Priority> ownerMaxPriorityTerritory = new Map<Id,Territory2Priority>();
        for(UserTerritory2Association ota:[Select UserId, Territory2Id, Territory2.Territory2Type.Priority from UserTerritory2Association where userId IN :ownerIds and Territory2.Territory2ModelId = :activeModelId]){
            System.debug('User: ' + ota.UserId + ' Territory: ' + ota.Territory2Id);
            Territory2Priority tp = ownerMaxPriorityTerritory.get(ota.UserId);
            
            if((tp == null) || (ota.Territory2.Territory2Type.Priority > tp.priority)){
                // If this is the first territory examined for the owner or it has greater priority than current highest priority territory, then set this as new highest priority territory.
                tp = new Territory2Priority(ota.Territory2Id,ota.Territory2.Territory2Type.priority,false);
            }else if(ota.Territory2.Territory2Type.priority == tp.priority){
                tp.moreTerritoriesAtPriority = true;
            }

            ownerMaxPriorityTerritory.put(ota.UserId, tp);
        }
        return ownerMaxPriorityTerritory;
    }

    private Id getActiveModelId() {
        List<Territory2Model> models = [Select Id from Territory2Model where State = 'Active'];
        Id activeModelId = null;
        if(models.size() == 1){
        activeModelId = models.get(0).Id;
        }
        return activeModelId;
    }

    private class Territory2Priority {
        public Id territory2Id { get; set; }
        public Integer priority { get; set; }
        public Boolean moreTerritoriesAtPriority { get; set; }
        
        Territory2Priority(Id territory2Id, Integer priority, Boolean moreTerritoriesAtPriority){
            this.territory2Id = territory2Id;
            this.priority = priority;
            this.moreTerritoriesAtPriority = moreTerritoriesAtPriority;
        }
    }
}