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
SF7SF7 

Automatically assign Opportunities owned by inactive user to their manager.

Hi,

 

I am looking for a way like when a user gets deactivated all the Opportunites owned by the user should be assigned to their manager .

 

How can i acheive this and any ideas. I know workflows cannot be used for this any thoughts?

 

Thanks

Akhil

Best Answer chosen by Admin (Salesforce Developers) 
SF7SF7

trigger AssignOpportunities2Manager on User (After Update) 
{   
    Map<Id, Id> usrMgrMap = new Map<Id, Id>();

    for(Integer i = 0; i < Trigger.new.size(); i++)
    {
        if(trigger.new[i].isActive == false && trigger.old[i].isActive == true)
        {
            if(trigger.new[i].ManagerId <> null)
            {
                usrMgrMap.put(trigger.new[i].Id, trigger.new[i].ManagerId);
            }
            else
            {
                // write code if you want to handle users with out a mangerId.
            }
        }
    }

    UpdateOpportunity.updateOpportunities(usrMgrMap);
}

 

global class UpdateOpportunity
{
    @future
    public static void updateOpportunities(Map<Id, Id> oppMap)
    {
        Set<Id> invalidUserIds = OppMap.keySet();
        List<Opportunity> OppList = new List<Opportunity>();
           
        for(Opportunity Opp : [SELECT Id, OwnerId FROM Opportunity WHERE OwnerId IN :invalidUserIds])
        {
            Opp.OwnerId = OppMap.get(Opp.OwnerId);
            OppList.add(Opp);
        }
        
        update OppList;
    }
}

All Answers

sivaextsivaext

Hi

 

Write on trigger on user

 

  trigger triggername on user  (after update) {

 

   List<Opportunity> opplist = new  List<Opportunity>();

   set<id> userids = new set<id>();

   

     for(User usr:Trigger.new) {

       if(usrActive = false) {

           userids.add(usr.id);

                

}

User u = [select id from user where role ='manager']; // identifying manager id, you can change where conditions

List<Opportunity> opplist1 = [select id, name, ownerid from opportunity where ownerid IN: userids];

for(Opportunity  opp:opplist1) {

      opp.ownerid = u.id;

   opplist.add(opp);

}

}

update opplist;

}

purupuru

Hi Akhil,

 

To acheive this you can write the after update trigger on object User.

In this trigger you can do below steps :

  1. Check IsActive field for the User.
  2. If IsActive field is false then fetch all the opprotunity own by this user.
  3. Select the Manger of this user.
  4. Assign Manager Id to OwnerId field of Opportunity.
  5. Update Opportunity.

So in this way when user is going to inactive you can assign all opportunity owned by this user to its manager.

 

Regards,

Purushottam

SF7SF7

Thanks Sivaext i will try this trigger and let you know.

SF7SF7

Thanks Purusotham I will try to implement the steps .

SF7SF7

trigger AssignOpportunities2Manager on User (After Update) 
{   
    Map<Id, Id> usrMgrMap = new Map<Id, Id>();

    for(Integer i = 0; i < Trigger.new.size(); i++)
    {
        if(trigger.new[i].isActive == false && trigger.old[i].isActive == true)
        {
            if(trigger.new[i].ManagerId <> null)
            {
                usrMgrMap.put(trigger.new[i].Id, trigger.new[i].ManagerId);
            }
            else
            {
                // write code if you want to handle users with out a mangerId.
            }
        }
    }

    UpdateOpportunity.updateOpportunities(usrMgrMap);
}

 

global class UpdateOpportunity
{
    @future
    public static void updateOpportunities(Map<Id, Id> oppMap)
    {
        Set<Id> invalidUserIds = OppMap.keySet();
        List<Opportunity> OppList = new List<Opportunity>();
           
        for(Opportunity Opp : [SELECT Id, OwnerId FROM Opportunity WHERE OwnerId IN :invalidUserIds])
        {
            Opp.OwnerId = OppMap.get(Opp.OwnerId);
            OppList.add(Opp);
        }
        
        update OppList;
    }
}

This was selected as the best answer