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
Sfdc WolfSfdc Wolf 

Need Test class for OpportunityTerritory2AssignmentFilter

Hi Folks,

Any one used this Class before 'OpportunityTerritory2AssignmentFilter' ?
It implements an interface 'TerritoryMgmt.OpportunityTerritory2AssignmentFilter' ,Its a sample class provided by salesforce!
Im trying to write a Test Class for this , 
Anyone has  test class for this Sample class ??
If anyone has worked on this before any help, references or suggestions are highly appreciaed ! :)
TIA
You can find this class here or below...

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_interface_TerritoryMgmt_OpportunityTerritory2AssignmentFilter.htm


 
/*** Apex version of the default logic.
* If opportunity's assigned account is assigned to
*  Case 1: 0 territories in active model
*            then set territory2Id =   null
*  Case 2: 1 territory in active model
*            then set territory2Id =   account's territory2Id
*  Case 3: 2 or more territories in active model
*            then set territory2Id =   account's territory2Id that is of highest priority.
*            But if multiple   territories have same highest priority, then set territory2Id = null 
*/
global class OppTerrAssignDefaultLogicFilter implements
TerritoryMgmt.OpportunityTerritory2AssignmentFilter {
     /**
     * No-arg constructor.
     */ 
     global   OppTerrAssignDefaultLogicFilter() {}

     /**
      * Get mapping of   opportunity to territory2Id. The incoming list of opportunityIds contains only those with IsExcludedFromTerritory2Filter=false.
      * If territory2Id =   null in result map, clear the opportunity.territory2Id if set.
      * If opportunity is not present in result map, its territory2Id remains intact.
      */
    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, AccountId, Territory2Id from Opportunity where Id   IN
              :opportunityIds];
            Set<Id>   accountIds = new Set<Id>();
            //Create   set of parent accountIds
            for(Opportunity opp:opportunities){
                if(opp.AccountId   != null){
                    accountIds.add(opp.AccountId);
                    }
                }

                Map<Id,Territory2Priority> accountMaxPriorityTerritory = 
getAccountMaxPriorityTerritory(activeModelId, accountIds);

            //for each opportunity, assign the highest priority territory if there is 
no conflict, else assign null
            for(Opportunity opp: opportunities){
               Territory2Priority tp = accountMaxPriorityTerritory.get(opp.AccountId);
               //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);
               }else{
                   OppIdTerritoryIdResult.put(opp.Id,   null);
               }
            }
        }
        return   OppIdTerritoryIdResult;
    }
    
    /**
      * Query assigned territoryIds in active model for given accountIds
      * Create a map of accountId to max priority territory 
      */
     private Map<Id,Territory2Priority> getAccountMaxPriorityTerritory(Id
activeModelId, Set<Id> accountIds){
        Map<Id,Territory2Priority> accountMaxPriorityTerritory = new 
Map<Id,Territory2Priority>();
        for(ObjectTerritory2Association ota:[Select ObjectId, Territory2Id, 
Territory2.Territory2Type.Priority from ObjectTerritory2Association where objectId IN 
:accountIds and Territory2.Territory2ModelId = :activeModelId]){
            Territory2Priority tp = accountMaxPriorityTerritory.get(ota.ObjectId);

            if((tp   == null) || (ota.Territory2.Territory2Type.Priority > 
tp.priority)){
                //If this is the first territory examined for account 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){
                //The priority of current highest territory is same as this, so set   
moreTerritoriesAtPriority to indicate multiple highest priority territories seen so far.
                tp.moreTerritoriesAtPriority = true;
            }
            
            accountMaxPriorityTerritory.put(ota.ObjectId,   tp);
        }
        return accountMaxPriorityTerritory;
    }

             
    /**
     * Get the Id of the Active Territory Model. 
     * If none exists, return   null;
     */
   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;
   }
   
   /**
    * Helper class to help   capture territory2Id, its priority, and whether there are more territories with same priority assigned to the
 account
    */
   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;
       }
   }
}}

 
Eswar Prasad@Sfdc11Eswar Prasad@Sfdc11
HI Vinay Nallakadi,
In above interface i will be writing a test class pls see below code

@istest
public class OppTerrAssignDefaultLogicFiltertest{
public static testmethod void oppTerrAssigntest(){

Opporunity opp =new Opporunity(name='testopporunity');
insert opp;
List<id> opplistid=new List<id>();
opplistid.add(opp.id);

Account acc =new Account(name='testaccount');
insert acc;
Set<id> acclistid=new Set<id>();
opplistid.add(acc.id);

//main class
OppTerrAssignDefaultLogicFilter oppcheck=new OppTerrAssignDefaultLogicFilter();
Map<Id,Id> mapid=oppcheck.getOpportunityTerritory2Assignments(opplistid);
Map<Id,Territory2Priority>mapterr=oppcheck.getAccountMaxPriorityTerritory(ab1234, acclistid)
Id id=oppcheck.getActiveModelId();
//inner class
OppTerrAssignDefaultLogicFilter.Territory2Priority innerclass=new OppTerrAssignDefaultLogicFilter.Territory2Priority(ab12,1234,true);
innerclass.territory2Id;
innerclass.priority;
innerclass.moreTerritoriesAtPriority;

}
}
I Hope it will help you.mark it as solution if problem is solve.
Regards,
Eswar Prasad.
Sfdc WolfSfdc Wolf
Hi Eswar Prasad,

Thanks for the response! I'm getting an error at Main Class, Line 3 "Invalid type: Territory2Priority" . Can you fix this? Any pre requisites on the configuration side?
Will BoyceWill Boyce
I am also trying to enable Territory assignment rules for opportunities with this code.  I am having issues with unexpected tokens.  Were either of you able to implement this code and have it work?