+ Start a Discussion
Abhishek Pal 33Abhishek Pal 33 

DML operation in User Territory Object?

How to use DML operation in User Territory Object in salesforce. I searched on Google and they said use salesforce SOAP API to resolve this. Does anyone has some example I didnt have much idea about it?

Thanks in advance.

-Abhishek
VineetKumarVineetKumar
I suppose the object name is a but different for the object when referring it using Apex:
I guess this is what you are looking for :
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/Territory2.htm

If not, can you let me know the requirement, as to what you are trying to do on user territory object?
Abhishek Pal 33Abhishek Pal 33

Code:-

  UserTerritory userTerritory= new UserTerritory();
            User userId=[Select Id from user where Alias=:test.Alias];
            Territory terrId=[Select Id from Territory where Name=:fieldValues[4]];
            userTerritory.UserId= userId.Id;
            userTerritory.TerritoryId=terrId.Id;
            insert userTerritory;  (Error DML operation not allowed on user territory object)
VineetKumarVineetKumar
Use below logic :
UserTerritory2Association userTerritory= new UserTerritory2Association();
User userId=[Select Id from user where Alias=:test.Alias LIMIT 1];
Territory2 terrId=[Select Id from Territory2 where Name=:fieldValues[4] LIMIT 1];
userTerritory.UserId= userId.Id;
userTerritory.Territory2Id=terrId.Id;
insert userTerritory;
system.debug('#### userTerritory = '+userTerritory);

 
Abhishek Pal 33Abhishek Pal 33
This object is part of API and if I am using the above code in anonymous block I am still getting the error that "sObject type 'Territory2' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names"

Above code is not working.  Apart from API if there is some other option from where I can insert userTerritory object please let me know.

 
VineetKumarVineetKumar
This is available in Apex as well. Is the Territory Management enabled in your org?
Abhishek Pal 33Abhishek Pal 33
Yup, it is enabled in my org. Could you please where do I need to check whether it is enabled or not?

I am checking in Setup-->ManageTerritories---->Settings-->
VineetKumarVineetKumar
Please check the below post for enabling it if not. Also check if your org is supported for enterprise territory management.
https://help.salesforce.com/apex/HTViewHelpDoc?id=tm2_enable_tm2.htm&language=en_US

 
Abhishek Pal 33Abhishek Pal 33
Hi Vineet,


The settings that you have mentioned above is enabled for my org. If I go in Setup---->Territories--->

I cant find any option Enable Enterprise Territory Management.  Please see the below screenshot for the same.



User-added imageUser-added image
RajivKumar BhatterRajivKumar Bhatter
@abhishek pal33@vineetkumar - I have the same requirement for assigning user to a territory through trigger/apex but I am getting same DML operation error. Did you guys found out a solution apart from Rest API
SrivathsaSrivathsa
Hi Bro, in my project also, we are trying to do user creation automation, and hence I came across this issue. yeah, DML is not allowed in few salesforce objects. People said you gotta do it through Rest API, but not enough solution how you gonna achieve it..
I have achieved it after many researches. here you go:

 
HttpRequest request = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
//system.debug('baseUrl-->'+baseUrl);
 
 request.setEndpoint(baseUrl+'/services/data/v48.0/sobjects/UserTerritory/');
request.setHeader('Content-Type', 'application/json; charset=UTF-8');
        request.setHeader('Accept', 'application/json');
request.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId());
request.setMethod('POST');
JSONGenerator gen = JSON.createGenerator(true);   
ID userIden = InsertUsers[0].ID;  // this part of code is not here. Its just inserted user ID.
ID terrIden = numOfID;  // Its territory ID you want to add to the user.
    gen.writeStartObject();      
    gen.writeStringField('UserId', userIden);  // these two fields are enough to create UserTerritory record, in a //way to tether User and a Territory.
    gen.writeStringField('TerritoryId', terrIden);
    
    gen.writeEndObject();    
    String jsonS = gen.getAsString();

request.setBody(jsonS);
res = http.send(request);