You need to sign in to do that
Don't have an account?
Nag 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.
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.
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.
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.
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);
}