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
Gene T.Gene T. 

Need Help on Trigger (Update Account owner when Contact owner is changed)

Is it possible to write a trigger that when an owner of a contact is change, the account owner is updated to the same owner as the contact? Also, if the account has multiple contacts associated with it, will the other contacts ownership be transferred as well? Any advice or tips would be much appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
TheIntegratorTheIntegrator

think this should do it

 

trigger changeOwner on Contact (after insert, after update) {
List <Id> AccountIds = new List <Id>();
List <Account> accounts = new List<Account>();
List <Contact> contacts = new List<Contact>();

List <Account> accSave = new List<Account>();
List <Contact> contSave = new List<Contact>();

for(Contact c:trigger.new ){
if(c.AccountId !=NULL)
AccountIds.add(c.AccountId);
}

accounts = [Select Id, OwnerId From Account Where Id IN :AccountIds];
contacts = [Select Id, AccountId, OwnerId From Contact Where AccountId IN :AccountIds and Id NOT IN :trigger.new];

for(Contact c:trigger.new ){
for(Account acc:accounts){
if(c.AccountId == acc.Id && c.OwnerId !=acc.OwnerId){
acc.OwnerId = c.OwnerId;
accSave.add(acc);
}
}

for(Contact con:contacts){
if(c.AccountId == con.AccountId && c.OwnerId != con.OwnerId){
con.OwnerId = c.OwnerId;
contSave.add(con);
}
}
}
update(accSave);
update(contSave);
}

All Answers

b-Forceb-Force

It is possible to change owner of account when contact owner will changed.

 

Are you sure about this implementation, because with one account there may be multiple contacts (with different owners).

 

Every time your account woner will get updated to the latest contact owner ? Are you sure with that ?

 

Thanks,

bForce

 

TheIntegratorTheIntegrator

think this should do it

 

trigger changeOwner on Contact (after insert, after update) {
List <Id> AccountIds = new List <Id>();
List <Account> accounts = new List<Account>();
List <Contact> contacts = new List<Contact>();

List <Account> accSave = new List<Account>();
List <Contact> contSave = new List<Contact>();

for(Contact c:trigger.new ){
if(c.AccountId !=NULL)
AccountIds.add(c.AccountId);
}

accounts = [Select Id, OwnerId From Account Where Id IN :AccountIds];
contacts = [Select Id, AccountId, OwnerId From Contact Where AccountId IN :AccountIds and Id NOT IN :trigger.new];

for(Contact c:trigger.new ){
for(Account acc:accounts){
if(c.AccountId == acc.Id && c.OwnerId !=acc.OwnerId){
acc.OwnerId = c.OwnerId;
accSave.add(acc);
}
}

for(Contact con:contacts){
if(c.AccountId == con.AccountId && c.OwnerId != con.OwnerId){
con.OwnerId = c.OwnerId;
contSave.add(con);
}
}
}
update(accSave);
update(contSave);
}

This was selected as the best answer
Gene T.Gene T.

b-Force - We're B2C, not B2B. We use accounts to group contacts in Families. That is why if a contact owner is changed, we need the account owner and associated contacts to be changed as well.

 

TheIntegrator - Thank you. It works perfectly, now I just need to figure out how to write the test code.

Gene T.Gene T.

TheIntegrator - I'm stil very new to this and have minimal coding experience. I'm stuck on writing the test code for this. Could you help me with it as well? I really have no idea where to start.

TheIntegratorTheIntegrator

Here is the test class for it, at the end there are assert statements to verify the owners are changed, for some reason they are not working. I'm in a rush so can't delve into why, will post later but this class should give you the coverage needed.

 

@isTest
private class Owner_Test {

    static testMethod void myUnitTest() {
        Profile p = [select id from profile where name='Standard User'];
 
        User user1 = new User(alias = 'test123', email='test123@noemail.com',
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p.Id, country='United States',
            timezonesidkey='America/Los_Angeles', username='test123@noemail.com');
        insert user1;
        
        User user2 = new User(alias = 'test456', email='test456@noemail.com',
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p.Id, country='United States',
            timezonesidkey='America/Los_Angeles', username='test456@noemail.com');
        insert user2;
        
        Account a = new Account(Name = 'Test Account', OwnerId=user1.Id);
        insert(a);
        Contact c1 = new Contact(AccountId=a.Id,lastname='Test1',firstname='Contact', OwnerId=user1.Id);
        insert(c1);
        Contact c2 = new Contact(AccountId=a.Id,lastname='Test2',firstname='Contact', OwnerId=user2.Id);
        insert(c2);
        //System.assertEquals(a.OwnerId,user2.Id);
        //System.assertEquals(c1.OwnerId,user2.Id);
    }
}

 

Gene T.Gene T.

Thanks! It worked perfectly. I really appreciate your help on this.

BobBob

How can I reverse this trigger so it makes the contact owner the same as the account owner?

BobBob

How can I flip this code to change the contact owner when it isn't the same as the account owner or when the account owner is changed?