Create a custom field called "Owner Copy"
Here's the trigger:
trigger accountOwnerCopy on Account (before Insert, before Update) {
// handle arbitrary number of Accounts
for(Account x : Trigger.New){
// Has Owner changed?
if (x.OwnerID != x.Owner_Copy__c) {
// check that owner is a user (not a queue)
if( ((String)x.OwnerId).substring(0,3) == '005' ){
x.Owner_Copy__c = x.OwnerId;
}
else{
// in case of Queue we clear out our copy field
x.Owner_Copy__c = null;
}
}
}
}
=====
You will also need this test class
@isTest
public with sharing class TriggerTestOwnerCopy {
static testMethod void testAccountOwnerCopy() {
// Grab two Users
User[] users = [select Id from User where IsActive= true limit 3];
User u1 = users[0];
User u2 = users[2];
// Create a Account
System.debug('Creating Account');
Account x1 = new Account(Name='Test', OwnerId = u1.Id);
insert x1;
// Test: Owner_Link should be set to user 1
Account x2 = [select id, OwnerId, Owner_Copy__c from Account where Id = :x1.Id];
System.assertEquals(u1.Id, x2.OwnerId);
System.assertEquals(u1.Id, x2.Owner_Copy__c);
// Modify Owner
x2.OwnerId = u2.Id;
update x2;
// Test: Owner_Link should be set to user 2
Account x3 = [select id, OwnerId, Owner_Copy__c from Account where Id = :x2.Id];
System.assertEquals(u2.Id, x3.OwnerId);
System.assertEquals(u2.Id, x3.Owner_Copy__c);
}
}