function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nag Raj 32Nag Raj 32 

How to insert test data of Territory2 and UserTerritory2Association in test class?

Hi friends,

I have a class which is using Territory2 and UserTerritory2Association objects.So i need to cover these lines.Can you please how to insert test data of territory2 and  UserTerritory2Association.

Thanks,
Raj.
Ekta KanderaEkta Kandera
Hi Nag Rag,
As far as I know, for Territory2, you can create Territory2 data but it will not be visible as it will not be in Active state. So, there is a workaround for this. You can use @IsTest (See All Data = true) in your test class and query it from the database.
Thanks.
Raj VakatiRaj Vakati

There is a way to create data for tests that are related to UserTerritory2Association's and you no need to use @seeAllData to true .There's now a UserTerritory Object that tracks User assignments to Territories. You'd no longer need to use SeeAllData=true to test this kind of functionality. It could be created from within a test class instead.

 
@isTest
private class SL_Test_TerritoyTriggerAndBatch {

    private static final Id ACCOUNT_BROKER_RECORDTYPE = [Select Id, DeveloperName from RecordType where sObjectType='Account' and DeveloperName='Broker' LIMIT 1].Id;

    private static testMethod void territorryAssignmentToAccountsByTrigger() {

        Account objAccount = new Account(RecordTypeId = ACCOUNT_BROKER_RECORDTYPE, Name = 'Test Acc - 01', ShippingState = 'USA');
        insert objAccount;
        User usr = [Select id from User where Id = :UserInfo.getUserId()];

        System.RunAs(usr)
        {
            Test.startTest();
               insertTestTerritory(objAccount.Id);
            Test.stopTest();
        }
    }

    //@future
    private static void insertTestTerritory(Id AccId)
    {
        List<Territory2Type> terriType   = [SELECT id, DeveloperName from Territory2Type where  DeveloperName = 'Broker_Assignments' LIMIT 1];
        List<Profile> adminProfile = [select id, name from Profile where  name = 'System Administrator'];

        Territory2Model terrModel = new Territory2Model();
        terrModel .DeveloperName='ModelName'; // required field
        terrModel.Name = 'Name'; // required field
        insert terrModel ;

        Territory2 objTerr = new Territory2(DeveloperName = 'TestTerritory', Territory2ModelId=terrModel.Id, Name='TestTerritory', Territory2TypeId=terriType[0].Id);
        insert objTerr;

        ObjectTerritory2Association objObjectTerritory2Association = new ObjectTerritory2Association(ObjectId = AccId, Territory2Id =objTerr.Id, AssociationCause='Territory2Manual' );
        insert objObjectTerritory2Association;

        Profile p = [SELECT id, Name FROM Profile where name = 'System Administrator' ].get(0);  
        User u = new User(firstname= 'Test',
                  lastname='XXXX',
                  Alias='Test',
                  email = 'test1234@test.com',
                  username= 'test1234xxx@test.com', 
                  profileId= p.id, 
                  emailencodingkey='UTF-8',
                  languagelocalekey='en_US',
                  localesidkey='en_US',
                  timezonesidkey='America/Los_Angeles');
        insert u;

        User u2 = new User(firstname= 'Test',
                  lastname='XXXX',
                  Alias='Test',
                  email = 'test1234122@test.com',
                  username= 'test1234xxx123@test.com', 
                  profileId= p.id, 
                  emailencodingkey='UTF-8',
                  languagelocalekey='en_US',
                  localesidkey='en_US',
                  timezonesidkey='America/Los_Angeles');
        insert u2;


        UserTerritory2Association objUserTerritory2Association = new UserTerritory2Association(Territory2Id= objTerr.Id, UserId= u.Id, RoleInTerritory2='Sales / Marketing Manager');
        insert objUserTerritory2Association;

        UserTerritory2Association objUserTerritory2Association2 = new UserTerritory2Association(Territory2Id= objTerr.Id, UserId= u2.Id, RoleInTerritory2='Sales / Marketing Administrator');
        insert objUserTerritory2Association2 ;


    }

}

 
Gaurav Singh 317Gaurav Singh 317
@future
    private static void insertTestTerritory(Id AccId)
    {
        List<Territory2Type> terriType   = [SELECT id, DeveloperName from Territory2Type where  DeveloperName = 'US' LIMIT 1];
        
        Territory2Model terrModel = new Territory2Model();
        terrModel .DeveloperName='ModelName'; // required field
        terrModel.Name = 'Test FMI'; // required field
        insert terrModel ;
        
        Territory2 objTerr = new Territory2(DeveloperName = 'TestTerritory', Territory2ModelId=terrModel.Id, Name='1234', Territory2TypeId=terriType[0].Id);
        insert objTerr;
        Profile prof = [select id from profile where name = 'FMI_CRM_User' LIMIT 1];  
        
        //Profile profq = [select id from profile where name = 'Medical' LIMIT 1]; 
        String orgId = UserInfo.getOrganizationId();
        User user = new User();
        user.firstName = 'test11';
        user.lastName = 'test21';
        user.profileId = prof.id;
        //user.profileId = profq.id;
        user.username = 'test1' + '@test1' + orgId + '.org';
        user.email = 'test1' + '@test1' + orgId + '.org';
        user.EmailEncodingKey = 'ISO-8859-1';
        user.Alias = 'test1';
        user.TimeZoneSidKey = 'America/Los_Angeles';
        user.LocaleSidKey = 'en_US';
        user.LanguageLocaleKey = 'en_US';
        insert user;
        PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name = 'FMI_Billing_User'];
        insert new PermissionSetAssignment(AssigneeId = user.id, PermissionSetId = ps.Id);
        system.runAs(user){
            ObjectTerritory2Association objObjectTerritory2Association = new ObjectTerritory2Association(ObjectId = AccId, Territory2Id =objTerr.Id, AssociationCause='Territory2Manual');
            insert objObjectTerritory2Association;
        }
           UserTerritory2Association userTerritory2Association = new UserTerritory2Association(Territory2Id =objTerr.Id, UserId =user.Id );
        insert userTerritory2Association;
        system.debug('userTerritory2Association***'+userTerritory2Association);
        
    }