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

Assign Territories in TM 2.0


We enabled ETM(or TM 2.0) in our Sandbox.
I am trying to insert AccountShare record for assigning territory to an account as below:

AccountShare a = new AccountShare(UserOrGroupId='00G17000000HBRdEAO', RowCause='TerritoryManual', AccountId ='0011700000C3Vua');
insert a;

But it is throwing following exception:

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: RowCause (cannot insert sharing row with this cause): [RowCause]

Before this, I successfully assigned territories from UI by clicking on 'Assign Territories' button under "Assigned Territories" list in Account detail page and was able to assign territory to the account. Also I can see the relevant AccountShare records created backend.

But I dont know why I am unable to do the same using Apex, i.e. inserting AccountShare records.

Please help.

Thanks a lot.
ShashankShashank (Salesforce Developers) 
You may have to change the Territory group settings for Account Access to atleast "Users in this territory can view and edit accounts assigned to this territory."