You need to sign in to do that
Don't have an account?
Allen2
Written the below piece of code but it's not working. Could anyone can correct me please?
Scenario:
When the owner of an Account is changed, check to see the Division of the new owner of the Account. If it is "Regional Sales", perform the following changes
Fo the Account, check the Open Opportunities that are owned by the previous owner of the Account, for each Open Opportunity set the owner to be the new owner of the Account
For the Account, check each Contact that is owned by the previous owner of the Account and set the owner to be the new owner of the Account
Apex Class:
public class AccountHandler (){
public static void updateOwner(List<Account> newAccountList, Map<Id, Account> oldMap){
Set<Id> accountIds = new Set<Id>();
List<opportunity> oppList = new List<opportunity>();
List<contact> conList = new List<contact>();
List<Account> AccountList = new List<Account>();
User usr = [Select Id, Division from User where isActive = true and Division = 'Regional Sales'];
for(Account acc : newAccountList) {
if(trigger.IsUpdate && oldMap!=null && oldMap.get(acc.Id).OwnerId != acc.OwnerId && acc.OwnerId == usr.Id){
accountIds.add(acc.Id);
}
}
if(!accountIds.IsEmpty()){
AccountList = [Select Id, OwnerId, Owner.Division from Account where Id =: accountIds];
for(Account acc : AccountList){
Opportunity opp = new Opportunity();
opp.AccountId = acc.Id;
opp.OwnerId = acc.OwnerId;
oppList.add(opp);
Contact con = new Contact();
con.AccountId = acc.Id;
con.OwnerId = acc.OwnerId;
conList.add(con);
}
}
if(oppList.size() > 0){
update oppList;
}
if(conList.size() > 0){
update conList;
}
}
}
Apex Trigger:
trigger AccountTrigger on Account (after insert, after update, before delete) {
if(Trigger.isUpdate && Trigger.isAfter){
AccountHandler.updateOwner(Trigger.New, Trigger.OldMap);
}
}
When the owner of an Account is changed, check to see the Division of the new owner of the Account. If it is "Regional Sales", perform the following changes
Fo the Account, check the Open Opportunities that are owned by the previous owner of the Account, for each Open Opportunity set the owner to be the new owner of the Account
For the Account, check each Contact that is owned by the previous owner of the Account and set the owner to be the new owner of the Account
Apex Class:
public class AccountHandler (){
public static void updateOwner(List<Account> newAccountList, Map<Id, Account> oldMap){
Set<Id> accountIds = new Set<Id>();
List<opportunity> oppList = new List<opportunity>();
List<contact> conList = new List<contact>();
List<Account> AccountList = new List<Account>();
User usr = [Select Id, Division from User where isActive = true and Division = 'Regional Sales'];
for(Account acc : newAccountList) {
if(trigger.IsUpdate && oldMap!=null && oldMap.get(acc.Id).OwnerId != acc.OwnerId && acc.OwnerId == usr.Id){
accountIds.add(acc.Id);
}
}
if(!accountIds.IsEmpty()){
AccountList = [Select Id, OwnerId, Owner.Division from Account where Id =: accountIds];
for(Account acc : AccountList){
Opportunity opp = new Opportunity();
opp.AccountId = acc.Id;
opp.OwnerId = acc.OwnerId;
oppList.add(opp);
Contact con = new Contact();
con.AccountId = acc.Id;
con.OwnerId = acc.OwnerId;
conList.add(con);
}
}
if(oppList.size() > 0){
update oppList;
}
if(conList.size() > 0){
update conList;
}
}
}
Apex Trigger:
trigger AccountTrigger on Account (after insert, after update, before delete) {
if(Trigger.isUpdate && Trigger.isAfter){
AccountHandler.updateOwner(Trigger.New, Trigger.OldMap);
}
}
Please try below one.
If need any other help or assistance you can directly contact me on my email id.
Thanks
Hemant
Email : sonihemant.jaipur@gmai.com
You can try this :
Please mark this as answer if it works for you.
Thanks