+ Start a Discussion
gowtham murugesangowtham murugesan 

Invalid type: UserTerritory2Association Kindly provide me a solution for this

Hi all Am facing this issue ,
here whats the mistake?

 set<Id> tIds = new set<Id>();
         List<UserTerritory2Association> lstUserTerritory = [SELECT Id, User.FirstName, User.LastName,Territory2.Name,Territory2Id, Territory2.Territory2Model.Name FROM UserTerritory2Association WHERE UserId =: userinfo.getUserId() and IsActive = True];
        system.debug('lstUserTerritory::::::'+lstUserTerritory);

            for(UserTerritory ut : lstUserTerritory) {
             if(lstUserTerritory != null && !lstUserTerritory.isEmpty()) {
                tIds.add(ut.TerritoryId);
            }
            system.debug('tIds::::::'+tIds);


am getting Invalid type: UserTerritory2Association How to fix this error ?
Khan AnasKhan Anas (Salesforce Developers) 
Hi Gowtham,

Greetings to you!

UserTerritory2Association will be available only if Enterprise Territory Management has been enabled for your organization. 

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_userterritory2association.htm

Enable Enterprise Territory Management:
  1. From Setup, enter Territories in the Quick Find box, and then select the territory settings.
  2. Click Enable Enterprise Territory Management.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas