8

When I tried to add territories field in soql

 list<Account> soql=[select id,name,Rating,Territories from account Where ownerid!=:UserInfo.getUserId()];

I ma getting following error.

No such column 'Territories' on entity 'Account'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 8 column 29

I could see the Territories field and when I try to get the values error coming.

What is the steps that I need to do..

7

"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...

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.