You need to sign in to do that
Don't have an account?
Curt Griffin 4
Trying to create OppTerrAssignDefaultLogicFilter Apex Class
When I try to create the Apex Class for this filter using this post, I get the following error when I try to save the Class:
Compile Error: unexpected token: 'global class OppTerrAssignDefaultLogicFilter implements' at line 11 column 0
I used the Apex Code example found at this link:
In case it is not obvious, I am not a devloper, so any suggestions would be appreciated.
Here is the sample code I used:
001/*** Apex version of the default logic.
002* If opportunity's assigned account is assigned to
003* Case 1: 0 territories in active model
004* then set territory2Id = null
005* Case 2: 1 territory in active model
006* then set territory2Id = account's territory2Id
007* Case 3: 2 or more territories in active model
008* then set territory2Id = account's territory2Id that is of highest priority.
009* But if multiple territories have same highest priority, then set territory2Id = null
010*/
011global class OppTerrAssignDefaultLogicFilter implements
012TerritoryMgmt.OpportunityTerritory2AssignmentFilter {
013 /**
014 * No-arg constructor.
015 */
016 global OppTerrAssignDefaultLogicFilter() {}
017
018 /**
019 * Get mapping of opportunity to territory2Id. The incoming list of opportunityIds contains only those with IsExcludedFromTerritory2Filter=false.
020 * If territory2Id = null in result map, clear the opportunity.territory2Id if set.
021 * If opportunity is not present in result map, its territory2Id remains intact.
022 */
023 global Map<Id,Id> getOpportunityTerritory2Assignments(List<Id> opportunityIds) {
024 Map<Id, Id> OppIdTerritoryIdResult = new Map<Id, Id>();
025
026 //Get the active territory model Id
027 Id activeModelId = getActiveModelId();
028
029 if(activeModelId != null){
030 List<Opportunity> opportunities =
031 [Select Id, AccountId, Territory2Id from Opportunity where Id IN
032 :opportunityIds];
033 Set<Id> accountIds = new Set<Id>();
034 //Create set of parent accountIds
035 for(Opportunity opp:opportunities){
036 if(opp.AccountId != null){
037 accountIds.add(opp.AccountId);
038 }
039 }
040
041 Map<Id,Territory2Priority> accountMaxPriorityTerritory =
042getAccountMaxPriorityTerritory(activeModelId, accountIds);
043
044 //for each opportunity, assign the highest priority territory if there is
045no conflict, else assign null
046 for(Opportunity opp: opportunities){
047 Territory2Priority tp = accountMaxPriorityTerritory.get(opp.AccountId);
048 //assign highest priority
049 territory if there is only 1
050 if((tp != null) && (tp.moreTerritoriesAtPriority == false) &&
051(tp.territory2Id != opp.Territory2Id)){
052 OppIdTerritoryIdResult.put(opp.Id, tp.territory2Id);
053 }else{
054 OppIdTerritoryIdResult.put(opp.Id, null);
055 }
056 }
057 }
058 return OppIdTerritoryIdResult;
059 }
060
061 /**
062 * Query assigned territoryIds in active model for given accountIds
063 * Create a map of accountId to max priority territory
064 */
065 private Map<Id,Territory2Priority> getAccountMaxPriorityTerritory(Id
066activeModelId, Set<Id> accountIds){
067 Map<Id,Territory2Priority> accountMaxPriorityTerritory = new
068Map<Id,Territory2Priority>();
069 for(ObjectTerritory2Association ota:[Select ObjectId, Territory2Id,
070Territory2.Territory2Type.Priority from ObjectTerritory2Association where objectId IN
071:accountIds and Territory2.Territory2ModelId = :activeModelId]){
072 Territory2Priority tp = accountMaxPriorityTerritory.get(ota.ObjectId);
073
074 if((tp == null) || (ota.Territory2.Territory2Type.Priority >
075tp.priority)){
076 //If this is the first territory examined for account or it has
077greater priority than current highest priority territory, then set this as new highest
078priority territory.
079 tp = new
080Territory2Priority(ota.Territory2Id,ota.Territory2.Territory2Type.priority,false);
081 }else if(ota.Territory2.Territory2Type.priority == tp.priority){
082 //The priority of current highest territory is same as this, so set
083moreTerritoriesAtPriority to indicate multiple highest priority territories seen so far.
084 tp.moreTerritoriesAtPriority = true;
085 }
086
087 accountMaxPriorityTerritory.put(ota.ObjectId, tp);
088 }
089 return accountMaxPriorityTerritory;
090 }
091
092
093 /**
094 * Get the Id of the Active Territory Model.
095 * If none exists, return null;
096 */
097 private Id getActiveModelId() {
098 List<Territory2Model> models = [Select Id from Territory2Model where State ='Active'];
099 Id activeModelId = null;
100 if(models.size() == 1){
101 activeModelId = models.get(0).Id;
102 }
103
104 return activeModelId;
105 }
106
107 /**
108 * Helper class to help capture territory2Id, its priority, and whether there are more territories with same priority assigned to the
109 account
110 */
111 private class Territory2Priority {
112 public Id territory2Id { get; set; }
113 public Integer priority { get; set; }
114 public Boolean moreTerritoriesAtPriority { get; set; }
115
116 Territory2Priority(Id territory2Id, Integer priority, Boolean moreTerritoriesAtPriority){
117 this.territory2Id = territory2Id;
118 this.priority = priority;
119 this.moreTerritoriesAtPriority = moreTerritoriesAtPriority;
120 }
121 }
122}}
Compile Error: unexpected token: 'global class OppTerrAssignDefaultLogicFilter implements' at line 11 column 0
I used the Apex Code example found at this link:
In case it is not obvious, I am not a devloper, so any suggestions would be appreciated.
Here is the sample code I used:
001/*** Apex version of the default logic.
002* If opportunity's assigned account is assigned to
003* Case 1: 0 territories in active model
004* then set territory2Id = null
005* Case 2: 1 territory in active model
006* then set territory2Id = account's territory2Id
007* Case 3: 2 or more territories in active model
008* then set territory2Id = account's territory2Id that is of highest priority.
009* But if multiple territories have same highest priority, then set territory2Id = null
010*/
011global class OppTerrAssignDefaultLogicFilter implements
012TerritoryMgmt.OpportunityTerritory2AssignmentFilter {
013 /**
014 * No-arg constructor.
015 */
016 global OppTerrAssignDefaultLogicFilter() {}
017
018 /**
019 * Get mapping of opportunity to territory2Id. The incoming list of opportunityIds contains only those with IsExcludedFromTerritory2Filter=false.
020 * If territory2Id = null in result map, clear the opportunity.territory2Id if set.
021 * If opportunity is not present in result map, its territory2Id remains intact.
022 */
023 global Map<Id,Id> getOpportunityTerritory2Assignments(List<Id> opportunityIds) {
024 Map<Id, Id> OppIdTerritoryIdResult = new Map<Id, Id>();
025
026 //Get the active territory model Id
027 Id activeModelId = getActiveModelId();
028
029 if(activeModelId != null){
030 List<Opportunity> opportunities =
031 [Select Id, AccountId, Territory2Id from Opportunity where Id IN
032 :opportunityIds];
033 Set<Id> accountIds = new Set<Id>();
034 //Create set of parent accountIds
035 for(Opportunity opp:opportunities){
036 if(opp.AccountId != null){
037 accountIds.add(opp.AccountId);
038 }
039 }
040
041 Map<Id,Territory2Priority> accountMaxPriorityTerritory =
042getAccountMaxPriorityTerritory(activeModelId, accountIds);
043
044 //for each opportunity, assign the highest priority territory if there is
045no conflict, else assign null
046 for(Opportunity opp: opportunities){
047 Territory2Priority tp = accountMaxPriorityTerritory.get(opp.AccountId);
048 //assign highest priority
049 territory if there is only 1
050 if((tp != null) && (tp.moreTerritoriesAtPriority == false) &&
051(tp.territory2Id != opp.Territory2Id)){
052 OppIdTerritoryIdResult.put(opp.Id, tp.territory2Id);
053 }else{
054 OppIdTerritoryIdResult.put(opp.Id, null);
055 }
056 }
057 }
058 return OppIdTerritoryIdResult;
059 }
060
061 /**
062 * Query assigned territoryIds in active model for given accountIds
063 * Create a map of accountId to max priority territory
064 */
065 private Map<Id,Territory2Priority> getAccountMaxPriorityTerritory(Id
066activeModelId, Set<Id> accountIds){
067 Map<Id,Territory2Priority> accountMaxPriorityTerritory = new
068Map<Id,Territory2Priority>();
069 for(ObjectTerritory2Association ota:[Select ObjectId, Territory2Id,
070Territory2.Territory2Type.Priority from ObjectTerritory2Association where objectId IN
071:accountIds and Territory2.Territory2ModelId = :activeModelId]){
072 Territory2Priority tp = accountMaxPriorityTerritory.get(ota.ObjectId);
073
074 if((tp == null) || (ota.Territory2.Territory2Type.Priority >
075tp.priority)){
076 //If this is the first territory examined for account or it has
077greater priority than current highest priority territory, then set this as new highest
078priority territory.
079 tp = new
080Territory2Priority(ota.Territory2Id,ota.Territory2.Territory2Type.priority,false);
081 }else if(ota.Territory2.Territory2Type.priority == tp.priority){
082 //The priority of current highest territory is same as this, so set
083moreTerritoriesAtPriority to indicate multiple highest priority territories seen so far.
084 tp.moreTerritoriesAtPriority = true;
085 }
086
087 accountMaxPriorityTerritory.put(ota.ObjectId, tp);
088 }
089 return accountMaxPriorityTerritory;
090 }
091
092
093 /**
094 * Get the Id of the Active Territory Model.
095 * If none exists, return null;
096 */
097 private Id getActiveModelId() {
098 List<Territory2Model> models = [Select Id from Territory2Model where State ='Active'];
099 Id activeModelId = null;
100 if(models.size() == 1){
101 activeModelId = models.get(0).Id;
102 }
103
104 return activeModelId;
105 }
106
107 /**
108 * Helper class to help capture territory2Id, its priority, and whether there are more territories with same priority assigned to the
109 account
110 */
111 private class Territory2Priority {
112 public Id territory2Id { get; set; }
113 public Integer priority { get; set; }
114 public Boolean moreTerritoriesAtPriority { get; set; }
115
116 Territory2Priority(Id territory2Id, Integer priority, Boolean moreTerritoriesAtPriority){
117 this.territory2Id = territory2Id;
118 this.priority = priority;
119 this.moreTerritoriesAtPriority = moreTerritoriesAtPriority;
120 }
121 }
122}}
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_interface_TerritoryMgmt_OpportunityTerritory2AssignmentFilter.htm