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
CliffACliffA 

What's the best way to manually assign an Account to the current user's territory?

I have an Apex class that's doing some behind the scenes duplicate detection on accounts/contacts.  The org has Territories enabled so the Apex code is operating at the system level so it can see across all territories.

 

Here's a brief description of what I want to do: Before a user adds a new contact we do a quick lookup using VisualForce and Apex.  If a matching account/contact is found in a territory that the user doesn't belong to I'd like to manually assign the matching Account to the user's territory.  What's the best way to:

  1. Determine the current user's territory via Apex? 
  2. Manually assign the Account to the user's territory via Apex?