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
chirugantachiruganta 

How to change Territory of an account in apex?

I need to change the territory of an account in apex class/ trigger. Is it possible? If yes, Please help me on this.
Ashish_SFDCAshish_SFDC
Hi Chiru, 

"Territories" appears to be a field but actually is something else (similar to how Billing / Shipping Address field can't be queried because it really consists of 5 inner fields and how Contact Name = Salutation + First Name + Last Name).

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_erd_territory.htm

You'll have to query through the sharing table AccountShare, then to Group (of a specific type) and then to Territory.

I don't have an org with Territories enabled handy at the moment to try actual queries but these boards posts might give you some ideas:

http://boards.developerforce.com/t5/Apex-Code-Development/how-do-i-get-the-territory-for-an-account-in-apex-api/td-p/84825

http://boards.developerforce.com/t5/Apex-Code-Development/Query-Account-Territory-help/td-p/197194

RowCause in AccountShare should be 'Territory Management','Territory' or 'Territory Manual', find the group id => go to group definition => find RelatedToId => that'll be the Territory Id. It's messy and you might be able to cut some corners with subqueries/parent queries but the whole setup is painful to retrieve with SOQL...

http://salesforce.stackexchange.com/questions/12460/territories-field-in-account

Regards,
Ashish
chirugantachiruganta
Thank you ashish. I need to update the Territory field of an account( to change the assighnment of account to other Territory). I am able to assign the account to other territory but it is not removing the previous territory assighnment.
Ashish_SFDCAshish_SFDC
Hi Chiru, 

See the Territory Update trigger on the below link , 

https://developer.salesforce.com/forums/ForumsMain?id=906F00000008vLTIAY

Regards,
Ashish
chirugantachiruganta
Hi Ashish,

In that link I didn't find the code to update the Territory of an account.