Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.17 KB | None | 0 0
  1. /*** Apex version of the default logic.
  2. * If opportunity's assigned account is assigned to
  3. *  Case 1: 0 territories in active model
  4. *            then set territory2Id = null
  5. *  Case 2: 1 territory in active model
  6. *            then set territory2Id = account's territory2Id
  7. *  Case 3: 2 or more territories in active model
  8. *            then set territory2Id = account's territory2Id that is of highest priority.
  9. *            But if multiple territories have same highest priority, then set territory2Id = null
  10. */
  11. global class OppTerrAssignDefaultLogicFilter implements TerritoryMgmt.OpportunityTerritory2AssignmentFilter {
  12.     /**
  13.      * No-arg constructor.
  14.      */
  15.      global OppTerrAssignDefaultLogicFilter() {}
  16.  
  17.      /**
  18.       * Get mapping of opportunity to territory2Id. The incoming list of opportunityIds contains only those with IsExcludedFromTerritory2Filter=false.
  19.       * If territory2Id = null in result map, clear the opportunity.territory2Id if set.
  20.       * If opportunity is not present in result map, its territory2Id remains intact.
  21.       */
  22.     global Map<Id,Id> getOpportunityTerritory2Assignments(List<Id> opportunityIds) {
  23.         Map<Id, Id> OppIdTerritoryIdResult = new Map<Id, Id>();
  24.  
  25.         // Get the active territory model Id
  26.         Id activeModelId = getActiveModelId();
  27.  
  28.         if(activeModelId != null){
  29.             List<Opportunity> opportunities =
  30.               [Select Id, AccountId, Territory2Id from Opportunity where Id IN :opportunityIds];
  31.             Set<Id> accountIds = new Set<Id>();
  32.             // Create set of parent accountIds
  33.             for(Opportunity opp:opportunities){
  34.                 if(opp.AccountId != null){
  35.                     accountIds.add(opp.AccountId);
  36.                     }
  37.                 }
  38.  
  39.                 Map<Id,Territory2Priority> accountMaxPriorityTerritory = getAccountMaxPriorityTerritory(activeModelId, accountIds);
  40.  
  41.             // For each opportunity, assign the highest priority territory if there is no conflict, else assign null.
  42.             for(Opportunity opp: opportunities){
  43.                Territory2Priority tp = accountMaxPriorityTerritory.get(opp.AccountId);
  44.                // Assign highest priority territory if there is only 1.
  45.               //if((tp != null) && (tp.moreTerritoriesAtPriority == false) && (tp.territory2Id != opp.Territory2Id)){
  46.               if((tp != null) && (tp.moreTerritoriesAtPriority == false) ){
  47.                    OppIdTerritoryIdResult.put(opp.Id, tp.territory2Id);
  48.                }else{
  49.                    OppIdTerritoryIdResult.put(opp.Id, null);
  50.                }
  51.             }
  52.         }
  53.         return OppIdTerritoryIdResult;
  54.     }
  55.    
  56.     /**
  57.       * Query assigned territoryIds in active model for given accountIds.
  58.       * Create a map of accountId to max priority territory.
  59.       */
  60.      private Map<Id,Territory2Priority> getAccountMaxPriorityTerritory(Id activeModelId, Set<Id> accountIds){
  61.         Map<Id,Territory2Priority> accountMaxPriorityTerritory = new Map<Id,Territory2Priority>();
  62.         for(ObjectTerritory2Association ota:[Select ObjectId, Territory2Id, Territory2.Territory2Type.Priority from ObjectTerritory2Association where objectId IN :accountIds and Territory2.Territory2ModelId = :activeModelId]){
  63.             Territory2Priority tp = accountMaxPriorityTerritory.get(ota.ObjectId);
  64.  
  65.             if((tp == null) || (ota.Territory2.Territory2Type.Priority > tp.priority)){
  66.                 // 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.
  67.                 tp = new Territory2Priority(ota.Territory2Id,ota.Territory2.Territory2Type.priority,false);
  68.             }else if(ota.Territory2.Territory2Type.priority == tp.priority){
  69.                 // The priority of current highest territory is same as this, so set moreTerritoriesAtPriority to indicate multiple highest priority territories seen so far.
  70.                 tp.moreTerritoriesAtPriority = true;
  71.             }
  72.            
  73.             accountMaxPriorityTerritory.put(ota.ObjectId, tp);
  74.         }
  75.         return accountMaxPriorityTerritory;
  76.     }
  77.  
  78.  
  79.     /**
  80.      * Get the Id of the Active Territory Model.
  81.      * If none exists, return null.
  82.      */
  83.     private Id getActiveModelId() {
  84.         List<Territory2Model> models = [Select Id from Territory2Model where State = 'Active'];
  85.         Id activeModelId = null;
  86.         if(models.size() == 1){
  87.             activeModelId = models.get(0).Id;
  88.         }
  89.  
  90.         return activeModelId;
  91.     }
  92.  
  93.     /**
  94.     * Helper class to help capture territory2Id, its priority, and whether there are more territories with same priority assigned to the account.
  95.     */
  96.     private class Territory2Priority {
  97.         public Id territory2Id { get; set; }
  98.         public Integer priority { get; set; }
  99.         public Boolean moreTerritoriesAtPriority { get; set; }
  100.  
  101.         Territory2Priority(Id territory2Id, Integer priority, Boolean moreTerritoriesAtPriority){
  102.             this.territory2Id = territory2Id;
  103.             this.priority = priority;
  104.             this.moreTerritoriesAtPriority = moreTerritoriesAtPriority;
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement