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
harsha vardhan vasa 9harsha vardhan vasa 9 

how to assign territory from old user to new user

HI guys,
i need to assign  territory on one user to another user. how can this be achieved in coding.please let know the solution and code if present or any references will be helpful.
Regards,
Harsha.
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Harsha,

Check the link below:
https://salesforce.stackexchange.com/questions/131033/assigning-user-to-a-territory
harsha vardhan vasa 9harsha vardhan vasa 9
HI Bhargavi,
Thanks for your time and effort,
my requirements is : whenever user is getting deactivated his territory and opportunities should be assigned to another user.
if related reference is available please let know.
Regards,
Harsha.
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Harsha,
Try the below code:
if(trigger.isAfter)
    {
        if(trigger.isUpdate)
        {
            
            List<Id> UserIds=new List<Id>();
            for(User u: Trigger.new)
            {
                if(u.isActive==false && trigger.OldMap.get(u.id).isActive!=u.isActive)
                {
                    UserIds.add(u.id); 
                }
            }
            List<UserTerritory2Association> userTerrList=new List<UserTerritory2Association>([SELECT Id, UserId, Territory2Id, IsActive FROM UserTerritory2Association where UserId in:UserIds]);
            for( UserTerritory2Association uTerr : userTerrList)
            {
                uTerr.UserId=otheruserId;
            }
            update userTerrList;
        }
    }

 
harsha vardhan vasa 9harsha vardhan vasa 9
Hi Bhargavi,
Thanks again:) 
i need to implement on VF page so that he can select the new user to which that territory needs to mapped.similarly i need to transfer opportunities as well. hope u got my requiremnt now.

Regards,
harsha,
8446546754.
Ajay K DubediAjay K Dubedi
Hi Harsha,
Adding Users to Territories
To add users to a territory:

1.From a territory detail page, click Add Users.
2.To show a filtered list of items, select a predefined list from the View drop-down list, or click Create New View to define your own custom views.
3.Select and deselect checkboxes in the Action column to move users between the Available and Selected areas. Also, you can:
-Click a letter to view users who correspond to that letter.
-Click All to display all users who match the criteria of the current view.
-Click select shown to move all users that are shown on the current list page to the Selected area below.
-Click deselect shown or deselect all to move users from the Selected area to the Available area.

Removing Users from Territories
You can remove a user from a territory and choose who will receive his or her open opportunities:

1.In the Assigned Users related list on the territory detail page, click Remove next to the user's name.
2.Click the New Owner lookup icon (Lookup icon) to select a recipient for any open opportunities the user may have.
3.Alternatively, leave the New Owner field blank. One of the following results will occur:
-If the parent territory has a forecast manager, then that forecast manager takes ownership of the user's open opportunities. Also, the forecast manager is assigned to the territory with Active in Territory unchecked.
-If the parent territory does not have a forecast manager, then the user remains in the territory with Active in Territory unchecked.
4.Click Save.
If very large amounts of data are affected, you will be sent a confirmation email when the operation has completed.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
harsha vardhan vasa 9harsha vardhan vasa 9
Hi Dubedi,
Thanks for the time.
i need to implement on the VF page.when user entered username his territory details will be appeared along with userlookup field. now user can select from the user lookup to whom he wants to map that territory. 
hope u understood my requirement.
Regards,
harsha
8446546754.