+ Start a Discussion
Vishnu7700Vishnu7700 

Trigger on account owner change

Hi,

when the owner of account is changed the realted list objects owner neeed to be changed.

Related objects like contact/opportunity/event/task/contract.

 

Can any help me out how to frame the logic to achive this funcationality.

 

Any help is highly appriciated.

 

Regards,

Vishnu.

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

In case you need to modify your code then do like this

 

for (Task t : act.Tasks) 

for (Task t : act.Events) 

 

and in the query like this

 

for (Account act : [SELECT Id, (SELECT Id, OwnerId FROM Contacts), (SELECT Id, OwnerId FROM Tasks),(SELECT Id, OwnerId FROM Events),(SELECT Id, OwnerId FROM Opportunities WHERE IsClosed = False) FROM Account WHERE Id in :accountIds])

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

All Answers

souvik9086souvik9086

Hi,

 

I have done it for Contact, Do same like for other related objects

 

trigger UpdateOwner on Account(after insert,after update){
List<Contact> cList = new List<Contact>();
Map<Id,Account> aMap = new Map<Id,Account>([SELECT ID, Name OwnerId,(Select id,name,ownerid from contacts) FROM Account WHERE Id =: Trigger.new.keyset()]);
for(Account a : Trigger.new){
for(Contact c : aMap.get(a.Id).Contacts){
c.OwnerId = a.OwnerId;
cList.add(c);
}
}
if(cList.size() > 0){
UPDATE cList;
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

souvik9086souvik9086

This is the full one

 

trigger UpdateOwner on Account(after insert,after update){
List<Contact> cList = new List<Contact>();
List<Opportunity> oList = new List<Opportunity>();
List<Event> eList = new List<Event>();
List<Task> tList = new List<Task>();
List<Contract> conTrList = new List<Contract>();
Map<Id,Account> aMap = new Map<Id,Account>([SELECT ID, Name OwnerId,(Select id,name,ownerid from contacts),(select id from Opportunities),(select id from tasks),(select id from events),(select id from contracts) FROM Account WHERE Id =: Trigger.new.keyset()]);
for(Account a : Trigger.new){
for(Contact c : aMap.get(a.Id).Contacts){
c.OwnerId = a.OwnerId;
cList.add(c);
}
for(Opportunity o : aMap.get(a.Id).Opportunities){
o.OwnerId = a.OwnerId;
oList.add(o);
}
for(Event e : aMap.get(a.Id).tasks){
e.OwnerId = a.OwnerId;
eList.add(e);
}
for(Task t : aMap.get(a.Id).events){
t.OwnerId = a.OwnerId;
tList.add(t);
}
for(Contract conTr : aMap.get(a.Id).contracts){
conTr.OwnerId = a.OwnerId;
conTrList.add(conTr);
}
}
if(cList.size() > 0){
UPDATE cList;
}
if(oList.size() > 0){
UPDATE oList;
}
if(eList.size() > 0){
UPDATE eList;
}
if(tList.size() > 0){
UPDATE tList;
}
if(conTrList.size() > 0){
UPDATE conTrList;
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Vishnu7700Vishnu7700

Hi,

Tried with below code but i'm getting error as "Compile Error: Invalid field Task for SObject Account at line 29 column 27"

Can any help me out how resolve the error.

Please find below trigger.

trigger reassignRelatedContactsnActivitesnOpportunities on Account (after update , after insert) {
 
      Set<Id> accountIds = new Set<Id>(); //set for holding the Ids of all Accounts that have been assigned to new Owners
      Map<Id, String> oldOwnerIds = new Map<Id, String>(); //map for holding the old account ownerId
      Map<Id, String> newOwnerIds = new Map<Id, String>(); //map for holding the new account ownerId
      Contact[] contactUpdates = new Contact[0]; //Contact sObject to hold OwnerId updates
      Opportunity[] opportunityUpdates = new Opportunity[0]; //Opportunity sObject to hold OwnerId updates
   Task[] taskUpdates = new Task[0]; //Task sObject to hold updates
   Event[] eventUpdates = new Event[0]; //Event sObject to hold updates
     
      for (Account a : Trigger.new) { //for all records
         if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId) {
            oldOwnerIds.put(a.Id, Trigger.oldMap.get(a.Id).OwnerId); //put the old OwnerId value in a map
            newOwnerIds.put(a.Id, a.OwnerId); //put the new OwnerId value in a map
            accountIds.add(a.Id); //add the Account Id to the set
         }
      }
     
      if (!accountIds.isEmpty()) { //if the accountIds Set is not empty
         for (Account act : [SELECT Id, (SELECT Id, OwnerId FROM Contacts), (SELECT Id, OwnerId FROM Task),(SELECT Id, OwnerId FROM Event),(SELECT Id, OwnerId FROM Opportunities WHERE IsClosed = False) FROM Account WHERE Id in :accountIds]) { //SOQL to get Contacts and Opportunities for updated Accounts
            String newOwnerId = newOwnerIds.get(act.Id); //get the new OwnerId value for the account
            String oldOwnerId = oldOwnerIds.get(act.Id); //get the old OwnerId value for the account
            for (Contact c : act.Contacts) { //for all contacts
               if (c.OwnerId == oldOwnerId) { //if the contact is assigned to the old account Owner
                  Contact updatedContact = new Contact(Id = c.Id, OwnerId = newOwnerId); //create a new Contact sObject
                  contactUpdates.add(updatedContact); //add the contact to our List of updates
               }
            }
   for (Task t : act.Task) { //for all tasks
    if(t.OwnerId == oldOwnerId){ //if the task is assigned to the old account owner
     Task updatedTask = new Task(Id = t.Id, OwnerId = newOwnerId); //create a new Task sObject
     taskUpdates.add(updatedTask); //add the task to our List of updates
    }
   }
   for (Event e : act.Evnet){//for all Events
    if(e.OwnerId ==oldOwnerId){ //if the event is assigned to the old account Owner
     Event updatedEvent = new Event(Id= e.Id, OwnerId = newOwnerId); //create a new Event sObject
     eventUpdates.add(updatedEvent); //add the event to our List of Updates
    }
   }
            for (Opportunity o : act.Opportunity) { //for all opportunities
               if (o.OwnerId == oldOwnerId) { //if the opportunity is assigned to the old account Owner
                  Opportunity updatedOpportunity = new Opportunity(Id = o.Id, OwnerId = newOwnerId); //create a new Opportunity sObject
                  opportunityUpdates.add(updatedOpportunity); //add the opportunity to our List of updates
               }
            }
         }
         update contactUpdates; //update the Contacts
   update taskUpdates; //update the Tasks
   update eventUpdates; //update the Events
         update opportunityUpdates; //update the Opportunities
      }
   
}

souvik9086souvik9086

Hello,

 

Try the above code of mine.

If not worked then we will look into the issue of your code.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

souvik9086souvik9086

In case you need to modify your code then do like this

 

for (Task t : act.Tasks) 

for (Task t : act.Events) 

 

and in the query like this

 

for (Account act : [SELECT Id, (SELECT Id, OwnerId FROM Contacts), (SELECT Id, OwnerId FROM Tasks),(SELECT Id, OwnerId FROM Events),(SELECT Id, OwnerId FROM Opportunities WHERE IsClosed = False) FROM Account WHERE Id in :accountIds])

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

This was selected as the best answer
Vishnu7700Vishnu7700

Thanks alot souvik9086 for u r timely help.

I assgined u kodos .

 

Thanks,

Vishnu.

Devender MDevender M
trigger UpdateOwnerOfrealtedObjects on Account(after update) {
set<Id> setOfAccoutOwnerChanged = new set<Id>();
for(Account a : Trigger.new){
if(a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId)
setOfAccoutOwnerChanged.add(a.Id);
}

if(!setOfAccoutOwnerChanged.isEmpty()) {
List<Contact> listOfContactToUpdate = new List<Contact>();
List<Opportunity> listOfOpportunityToUpdate = new List<Opportunity>();
List<Event> listOfEventToUpdate = new List<Event>();
List<Task> listOfTaskToUpdate = new List<Task>();
List<Contract> listOfContractToUpdate = new List<Contract>();

Map<Id,Account> accountMap = new Map<Id,Account>([SELECT ID, Name OwnerId,(Select id,name,ownerid from contacts),(select id from Opportunities),(select id from tasks),(select id from events),(select id from contracts) FROM Account WHERE Id =:setOfAccoutOwnerChanged]);

for(Account a : accountMap.keyset()){

for(Contact contact : accountMap.get(a.Id).Contacts){
contact.OwnerId = a.OwnerId;
listOfContactToUpdate.add(contact);
}

for(Opportunity opportunity : accountMap.get(a.Id).Opportunities){
opportunity.OwnerId = a.OwnerId;
listOfOpportunityToUpdate.add(opportunity);
}

for(Event event : accountMap.get(a.Id).tasks){
event.OwnerId = a.OwnerId;
listOfEventToUpdate.add(event);
}

for(Task task : accountMap.get(a.Id).events){
task.OwnerId = a.OwnerId;
listOfTaskToUpdate.add(task);
}

for(Contract contract : accountMap.get(a.Id).contracts){
contract.OwnerId = a.OwnerId;
listOfContractToUpdate.add(contract);
}
}
if(!listOfContactToUpdate.isEmpty()){
Update listOfContactToUpdate;
}
if(!listOfOpportunityToUpdate.isEmpty()){
Update listOfOpportunityToUpdate;
}
if(!listOfEventToUpdate.isEmpty()){
Update listOfEventToUpdate;
}
if(!listOfTaskToUpdate.isEmpty()){
Update listOfTaskToUpdate;
}
if(!listOfContractToUpdate.isEmpty()){
Update listOfContractToUpdate;
}
}
}
Prashant Chandanshiv 1Prashant Chandanshiv 1
Hi souvik9086,
I am facing this Error at Line No. 3 : " Method does not exist or incorrect signature: void keyset() from the type List<Account "

trigger UpdateOwner on Account(after insert,after update){
List<Contact> cList = new List<Contact>();
Map<Id,Account> aMap = new Map<Id,Account>([SELECT ID, Name OwnerId,(Select id,name,ownerid from contacts) FROM Account WHERE Id =: Trigger.new.keyset()]);
for(Account a : Trigger.new){
for(Contact c : aMap.get(a.Id).Contacts){
c.OwnerId = a.OwnerId;
cList.add(c);
}
}
if(cList.size() > 0){
UPDATE cList;
}
}