I want to show an error message when admin tries to associate the user of a particular profile with more than one territory. The user of this profile should be assigned to one territory only. If the user should have one territory if he belongs to a particular profile How can I achieve this?


So to answer your query, I will give you a brief on what I know about territory management. Whenever you add a user to a territory a record gets created in the object UserTerritory2Association.

The UserTerritory2Association object will have a mapping of the Territory2Id and UserId

So you will have to restrict the record creation when a second entry is being created for a particular user.

You can achieve it by using a trigger like below, which checks whether the user being inserted is already present in the UserTerritory2Association object.

Below trigger code is for reference. You can modify it, to write in a better way using a handler class.

trigger MyUserTerritoryAssociationTrigger on UserTerritory2Association(before insert) {
    //Trigger logic
    Set<Id> userIds = new Set<Id>();
    Map<Id,UserTerritory2Association> mapUserTerritoryMap = new Map<Id,UserTerritory2Association>();
    for(UserTerritory2Association u: Trigger.new) {

    for(UserTerritory2Association uTObj: [SELECT Id, UserId FROM UserTerritory2Association WHERE UserId IN :userIDs]){
        mapUserTerritoryMap.put(uTObj.UserId, uTObj);

    for(UserTerritory2Association u: Trigger.new) {
        if(mapUserTerritoryMap.containsKey(u.UserId)) {
            u.addError('User is already a member of another Territory');


See if this helps!

| improve this answer | |
  • Hi Hemant, But there is no object Like "UserTerritory2Association" . UserTerritory and Territory object is there. So I am not sure on which object I have to write trigger. Can you please help on this – Jay Feb 8 '18 at 7:24
  • UserTerritory2Associationobject is present if enterprise territory management is enabled. I suppose in your case the territory and user mapping would be stored in UserTerritoy object. But I suppose salesforce doesn't allow a trigger on UserTerritory object. – Hemant Jain Feb 8 '18 at 9:22

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.