-1

I am not able to cover the lines for the below class which is in bold(four stars) and method called "getAccountMaxPriorityTerritory", can anyone help me how to do that please.

Below is the method which i am not able to cover.

    @TestVisible 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]){
            system.debug('@@@'+ota);
            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;
    }

Below is my entire class

     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, 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);
                        system.debug('@@@'+OppIdTerritoryIdResult);
                    }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.
    */
       ****@TestVisible 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.
    */

       @TestVisible 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.
    */


        @TestVisible private class Territory2Priority {
            public Id territory2Id { get; set; }
            public Integer priority { get; set; }
            public Boolean moreTerritoriesAtPriority { get; set; }
           @testVisible Territory2Priority(Id territory2Id, Integer priority, Boolean moreTerritoriesAtPriority){
                this.territory2Id = territory2Id;
                this.priority = priority;
                this.moreTerritoriesAtPriority = moreTerritoriesAtPriority;
            }
        }
    }

Below is my test class:

    @isTest
    public class TerrAssignDefaultFiltertest{

        @isTest
        static void unitTest() {
            Id opportunityId = [Select Id From Opportunity].Id;



            TerrAssignDefaultLogicFilter filter = new TerrAssignDefaultLogicFilter ();

            Test.startTest();

            Map<Id, Id> resultMap = filter.getOpportunityTerritory2Assignments(new List<Id> {opportunityId});
            TerrAssignDefaultLogicFilter.Territory2Priority tp = new TerrAssignDefaultLogicFilter.Territory2Priority(opportunityId, 1, false);
            Test.stopTest();

            System.assertEquals(1, resultMap.size());
            System.assertEquals(tp.territory2Id, opportunityId);
            System.assertEquals(1, tp.priority);
            System.assertEquals(false, tp.moreTerritoriesAtPriority);
        }

        @testSetup
        static void setup() {
            Account acc=new Account();
            acc.Name='acc1'; 
            insert acc;

            opportunity opp =new opportunity(name='testopporunity');
            insert opp;


        }
     }
  • @identigral : I am not able to cover a particular method so i am seeking help – learner Nov 13 '19 at 8:50
0

I think you should pass account id to the opportunity you inserted in the testSetup method in your test class.

Set<Id> accountIds is null in getOpportunityTerritory2Assignments method because you didn't pass the accountId in the testSetup method.

You can pass accountId = acc.Id in the opportunity while insertng.

    @testSetup
    static void setup() {
        Account acc=new Account();
        acc.Name='acc1'; 
        insert acc;

        opportunity opp =new opportunity(name='testopporunity', accountId = acc.Id);
        insert opp;
    }

@razzak, below is the code which i have modified

Id opportunityId = [Select Id From Opportunity].Id;
       set<id> accountidset = new set <id>();
        for(account a:[Select Id From account limit 10])
        {
         accountidset.add(a.id);
         }

        Id Territaries = [SELECT id, DeveloperName from Territory2 limit 1].Id;
filter.getAccountMaxPriorityTerritory(terriType,accountidset);

In @testSetup

opportunity opp =new opportunity(name='testopporunity',accountId = acc.Id);
        insert opp;
0

I have written the below code still not working

@isTest
public class TerrAssignDefaultFiltertest{

  @isTest
    static void unitTest() {
        Id opportunityId = [Select Id From Opportunity].Id;
       set<id> accountidset = new set <id>();
        for(account a:[Select Id From account limit 10])
        {
         accountidset.add(a.id);
         }

        Id Territaries = [SELECT id, DeveloperName from Territory2 limit 1].Id;
        TerrAssignDefaultLogicFilter filter = new TerrAssignDefaultLogicFilter ();

        Test.startTest();

        Map<Id, Id> resultMap = filter.getOpportunityTerritory2Assignments(new List<Id> {opportunityId});
        TerrAssignDefaultLogicFilter.Territory2Priority tp = new TerrAssignDefaultLogicFilter.Territory2Priority(opportunityId, 1, false);
          filter.getAccountMaxPriorityTerritory(terriType,accountidset);
        Test.stopTest();

        System.assertEquals(1, resultMap.size());
        System.assertEquals(tp.territory2Id, opportunityId);
        System.assertEquals(1, tp.priority);
        System.assertEquals(false, tp.moreTerritoriesAtPriority);
    }

    @testSetup
    static void setup() {
        Account acc=new Account();
        acc.Name='acc1'; 
        insert acc;

        opportunity opp =new opportunity(name='testopporunity',accountId = acc.Id);
        insert opp;


    }
   }
  • Welcome to Salesforce SE! Please use the Post answer button only for actual answers. You should modify your original question to add additional information. – identigral Nov 13 '19 at 15:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.