Can I invoke from another apex class, lwc or flow, ready-made filter class by salesforce (which described here)?

I tried invoke it through anonymous window in dev console, used next lines of code:

List<Id> oppsId = new List<Id>();
List<Opportunity> opps = [Select Id From Opportunity Where Id = 'XXXXXXX'];


OppTerrAssignDefaultLogicFilter filter = new OppTerrAssignDefaultLogicFilter();

But after I run this code, selected opportunity didn't assign to territory. Maybe I invoke it wrong or we can't invoke it like this?

If I click on the "filter" button in the territory setup - everyting works well. So territories set up well

1 Answer 1


The getOpportunityTerritory2Assignments method of the OppTerrAssignDefaultLogicFilter class doesn't update the db. Instead, it returns a map of the results that you should use.

Try something like this:

List<Id> opportunityIds = new List<Id>();
List<Opportunity> opps = [Select Id From Opportunity Where Id = 'XXXXXXX'];

OppTerrAssignDefaultLogicFilter oppTerrAssignFilter = new OppTerrAssignDefaultLogicFilter();
Map<Id, Id> opportunityTerritoryAssignments = oppTerrAssignFilter.getOpportunityTerritory2Assignments(opportunityIds);

for (Id opportunityId : opportunityTerritoryAssignments.keySet()) {
    System.debug('Opportunity Id: ' + opportunityId + ', Assigned Territory2 Id: ' + opportunityTerritoryAssignments.get(opportunityId));
  • Thank you so much, I understood! Then I need just to update Territory2Id on the opp, to see changes, correct?
    – Bob Greed
    Commented Feb 12, 2023 at 19:40
  • 1
    It depends on where and why you are doing it. But in general I would say so.
    – gvgramazio
    Commented Feb 12, 2023 at 19:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .