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
cmarkcmark 

how do i get the territory for an account (in apex/api)?

How do I query the Territories that are associated with an Account?  I found another thread that mentioned the AccountShare object, but I don't see how that helps...

Thanks
Chris
SiriusBlackOpSiriusBlackOp
At a glance, and if the AccountShare object is the one you need, then it would seem that you are talking about Groups.
 
AccountShare.UserOrGroupId will lead you to users or groups, and groups will eventually lead to users (though it may lead you back into another group before you get the the users).
 
AccountShare.AccountID will obviously get you to the accounts with permission from the users found in the above.
cmarkcmark
Not quite sure what you mean - I'm not looking for Groups - I'm looking for Territories.  Not certain that the AccountShare object is what I need, but I saw a reference to that in another post.  Furthermore the RowCause attribute on that object has a picklist value of Territory.  But even when the value is Territory, I'm not sure what to do with the UserOrGroupId value.

thanks
chris
Drew1815Drew1815
You will need to query the AccountShare, Group, and the Territory objects to figure out what Territory name(s) are associated to a specific Account.

For example (psuedo-code):
1. First query the AccountShare object.
AccountShare[] = Select Id, UserOrGroupId from AccountShare where RowCause = 'Territory' and AccountId = {account you want to get the list of territories for}

2. Second, query the Group object.
Group[] = Select Id, RelatedId from Group where Type='Territory' and Id IN (list of AccountShare.Ids retrieved in the first query)

3. Third, query the Territory object to get the Territory Names.
Territory[] = select id, name from Territory where Id IN (list of Group.RelatedIds retrieved in previous query)

I hope this helps.

-Andrew
SiriusBlackOpSiriusBlackOp
Interesting... I don't have a "Territory" object in either my Enterprise or Development instances.  Is that from an AppExchange application or something?
Drew1815Drew1815
The Territory object is only visible if you have enabled Territory management functionality within your org. Please note that you can not disable Territory Management functionality once its enabled in an org so please use caution!
You also will need System Administrator level of access to see those objects if enabled.
cmarkcmark
Hello Andrew.

That's the info that I needed - thanks.
chris
BrianW4123BrianW4123
Does anyone know how to query this information in a single SOQL statement?
 
Is it possible to use the relationships between AccountShare and Group to combine the statements?  I have not been able to figure out the relationship name (since it is using the UserOrGroupID and can reference to two objects).
 
Thanks,
Brian
 
gbalakrigbalakri

I am in a situtation where I need to detect the change in the Account territory in Apex. Any idea how this can be done? Based on this particular change I need to update the sharing model of other related objects

 

Girish

 

Bard09Bard09

BrianW4123 wrote:
Does anyone know how to query this information in a single SOQL statement?
 
Is it possible to use the relationships between AccountShare and Group to combine the statements?  I have not been able to figure out the relationship name (since it is using the UserOrGroupID and can reference to two objects).
 
Thanks,
Brian
 

 

I'm interested in a cleaner/more concise way of querying this as well.  From this thread I've mocked up some code that should work at pulling up all of an Account's Territories, but it's really dreadful in terms of nested SOQL queries.  Here is an example (this is untested code):

 

 

for (AccountShare AccountShareList :[Select Id,UserOrGroupId FROM AccountShare WHERE AccountId = '0015000000NJvDg' AND RowCause = 'Territory']) { for (Group GroupList :[Select Id,RelatedId FROM Group WHERE Id = :AccountShareList.UserOrGroupId) { for (Territory TerritoryList :[Select Id,Name,ForecastUserId FROM Territory WHERE Id = GroupList.RelatedId]) {

 

ETC

 

} } }

 

 So-- anyone got a creative/elegant way of simplifying these queries?

 

tstrongtstrong

If this trigger is trying to detect changes in Territory and it is based on the AccountShare and Group objects, what object is the trigger on?  I can't figure out how to write a trigger on a "hidden" object either through the UI or Eclipse.

 

I have a requirement to send an email to the user listed on the territory when an account is added to that territory.  Any idea how that would work?

Praveen KimarPraveen Kimar

Hi Andrew,

 

We are getting only rule based territory assignments related to an account but not the manually added territories with the query.

Could you let us know how can we get the manually assigned territories related to an account.

 

Thanks in Advance...

 

Regards,

Praveen K. 

Sheikh Abrar Ul HaqSheikh Abrar Ul Haq

Below is an algorithm to get the territory for an account:

 

Algorithm (Pseudo Code):
1) Query AccountShare object where AccountId = Trigger.New (Account Ids)
where RowCause = 'Territory' (Note: If Account has assigned via Territory Assignment Rules).
where RowCause = 'TerritoryManual' (Note: If Account has assigned via “Manually Assigned Accounts” related list on Territory detail page or AccountShare record has created in Apex Code).
2) Query Group object where Id = AccountShare.UserOrGroupId
3) Query Territory object where Id = Group.RelatedId
4) Query UserTerritory object where Id = Territory.UserId

Also, Below is the link for further details:

http://sforcecloud.blogspot.com/2013/05/territory-management-in-apex.html

 

Thanks,

Abrar Haq

chirugantachiruganta
How to change the territory of an account in apex?
jharna_dev1.3972192506744717E12jharna_dev1.3972192506744717E12
Hi Andrew,

I am able to figure out what Territory name(s) are associated to a specific Account by querying objects - AccountShare, Group, and the Territory.
How to write test methods for the same?

Thanks
Sapna KumariSapna Kumari
Hi Andrew,

I want to query the territory name associated with the billing postal code(mentioned in rule assignmnet) of the account while converting the lead. I want to change the owner of the converting lead to the user assigned with related territory.

help will be appriciated as soon as possible.
Meredith JenMeredith Jen
Hi,

Could anyone help me how "Territories" field on account can be pulled. I see in our instance that field is manually assigned through automation. I am not sure how that is possible. Please help me!!

Thanks. 
Loran SagguLoran Saggu
You can query the ObjectTerritory2Association table to find relationships between accounts and territories. ObjectTerritory2Association is a junction object between Account and Territory.

See documentation here: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_objectterritory2association.htm