You need to sign in to do that
Don't have an account?
cmark
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
Thanks
Chris
thanks
chris
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
You also will need System Administrator level of access to see those objects if enabled.
That's the info that I needed - thanks.
chris
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
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?
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?
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.
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
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
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.
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.
See documentation here: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_objectterritory2association.htm