You need to sign in to do that
Don't have an account?
SF7
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
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
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;
}
Hi Akhil,
To acheive this you can write the after update trigger on object User.
In this trigger you can do below steps :
So in this way when user is going to inactive you can assign all opportunity owned by this user to its manager.
Regards,
Purushottam
Thanks Sivaext i will try this trigger and let you know.
Thanks Purusotham I will try to implement the steps .
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;
}
}