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
Jose Bernal 2Jose Bernal 2 

Territory2Type and Territory2Model creation for testing

Hi guys,

I'm trying to create objects of Territory2Type and Territory2Model classes but it says that DML is not allowed. I also tried SOAP calls but these are not allowed for testing. What should I do?

Best regards,

Best Answer chosen by Jose Bernal 2
Sneha1991Sneha1991
Hi Jose Bernal,

Are you trying to insert Territory2Type and Territory2Model records in Test class ? No need to insert records, you can directly query it in test class. It is accessible in test classes.

For Ex : -  list<Territory2Type> terriType   = [SELECT id, DeveloperName from Territory2Type where  DeveloperName = 'Territory'];
DML is not allowed for Territory2Type but you can perform DML on Territory2Model.

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

Thanks

All Answers

Sneha1991Sneha1991
Hi Jose Bernal,

Are you trying to insert Territory2Type and Territory2Model records in Test class ? No need to insert records, you can directly query it in test class. It is accessible in test classes.

For Ex : -  list<Territory2Type> terriType   = [SELECT id, DeveloperName from Territory2Type where  DeveloperName = 'Territory'];
DML is not allowed for Territory2Type but you can perform DML on Territory2Model.

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

Thanks
This was selected as the best answer
Jose Bernal 2Jose Bernal 2
Thanks for your response. I could insert a Territory2Model using runAs:
System.runAs(createMockUserForProfile('System Administrator')) {
  model = new Territory2Model(
  Name = name,
  DeveloperName = name);

  insert model;

  System.assert(model != null);
}

However, Territory2Type is not as easy to create. Would there be any option for that?

Thanks,
Sneha1991Sneha1991
DML is not allowed on Territory2Type object so you can upload it in static resource and use it.
For ex :-
List<sObject> listTerritoryType = Test.loadData(Territory2Type.sObjectType, 'TerritoryType');
or you can create manually in org and can query it.

Thanks
Jose Bernal 2Jose Bernal 2
Yes, it works! Thank you so much Sneha1991 :)