You need to sign in to do that
Don't have an account?
Joel Richards
OppTerrAssignDefaultLogicFilter class
Hi, I'm trying to Enable Filter-Based Opportunity Territory Assignment for Enterprise Territory Management.Part of the setup is:
'If your organization is using the Apex code Salesforce provides, first create the class and then return to this Settings page and enter the class name: OppTerrAssignDefaultLogicFilter. If you opt to use your own code for the Apex class, you’ll come back and enter the name of the class that you create.
However I can't find any other information related to the 'OppTerrAssignDefaultLogicFilter' class anywhere. Has anyone come across this apex class and if so can they share it?
'If your organization is using the Apex code Salesforce provides, first create the class and then return to this Settings page and enter the class name: OppTerrAssignDefaultLogicFilter. If you opt to use your own code for the Apex class, you’ll come back and enter the name of the class that you create.
However I can't find any other information related to the 'OppTerrAssignDefaultLogicFilter' class anywhere. Has anyone come across this apex class and if so can they share it?
All Answers
Did you get the OppTerrAssignDefaultLogicFilter to work in your org?
Was this issue ever resolved? The OppTerrAssignDefaultLogicFilter apex class in not working in my dev org, keep getting an unexpected token 'else' error and I am using the code Salesforce provided. Any update would be appreciated! Thank you!!
@Jennifer Alden Sanchez, that Apex class needs to be edited. There are several comments that wrap to two different lines, without the comment delimiter in front of the second line. The first error is that "else" on line 45. Let me know if you want some help with it. Here is another post on the same topic.
https://developer.salesforce.com/forums/?id=906F0000000BJSZIA4
Compile Error: unexpected token: 'global class OppTerrAssignDefaultLogicFilter implements' at line 11 column 0
I used the Apex Code example found at this link:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_interface_TerritoryMgmt_OpportunityTerritory2AssignmentFilter.htm
I editted the above Apex Class to fix the issue with the comments wrapping to another line...I think I got them all. but I still get the above error. See the Apex Class code below.
In case it is not obvious, I am not a devloper, so any suggestions would be appreciated.
Thank you
Curt
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_interface_TerritoryMgmt_OpportunityTerritory2AssignmentFilter.htm
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;
}
}
}