I want to get user and user's territory information. After i search, i found that user information in user Table. territory information in Territory Table. but, the relationship in UserTerritory Table. But, from the apex guide,i found that UserTerritory Don’t Support DML Operations. So, i can not get the information. So, Does anyone knows that how to write this soql or would you please give some idea to address this issue.

Many thanks


You have to do this separately, you can't traverse like that through the territory objects, they're "special". So get the User's Territories like this:

Id currentUserId = 'theuseridgoeshere';
List<UserTerritory> myList = [SELECT Id, UserId, TerritoryId FROM UserTerritory WHERE UserId =: currentUserId];

Build a collection of the TerritoryId values:

Set<Id> territoryIds = new Set<Id>();
for(UserTerritory terr : myList) {

Then go query the Territory data you want:

Map<Id, Territory> = idToTerritoryMap = new Map<Id, Territory>([SELECT Id, Name FROM Territory WHERE Id IN : territoryIds]);

It might not support DML, but it should still support query, try something like

select Territory.Name FROM UserTerritory WHERE UserId IN (select id from User where name = 'John Doe')

  • I try this sql in salesforce. I get error. INVALID_FIELD: select Territory.Name FROM UserTerritory ^ ERROR at Row:1:Column:8 Didn't understand relationship 'Territory' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
    – yutaolife
    Jul 7 '13 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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