1) Create a lookup field “Previous Owner” in Account as LookUp(User)
2) Create a trigger like this:
trigger OnAccountOwnerChanged on Account (before update) { for( Account a : Trigger.new ) { Account AcctBeforeUpdate = Trigger.oldMap.get( a.Id ); if( AcctBeforeUpdate.OwnerId != a.OwnerId && ((String) a.OwnerId).startsWith( '005' ) ) a.Previous_Owner__c = AcctBeforeUpdate.OwnerId; } }
3) Use the “Previous Owner” in your reports and formulas. It will let you reference the user name since it is a lookup field (eg.: Previous_Owner__r.LastName + ‘, ‘ + Previous_Owner__r.FirstName).
4) The part that says
&& ((String) a.OwnerId).startsWith( '005' )
is for when the owner changes from one user to a queue. It will ignore queues as owners when setting the previous owner..
5) Create a test class like this:
public with sharing class TestTriggerOnAccountOwnerChanged { public static TestMethod void TestEverything() { // get default profile Profile objP = [ SELECT Id FROM Profile WHERE Name='Standard User' ]; // create 2 users User objU1 = new User( Alias = 'TTOAOC1', Email='TTOAOC1@testorg.com' , CommunityNickName = 'TTOAOC1', EmailEncodingKey='UTF-8' , UserName='TestingTTOAOC1@testorg.com', LastName = 'TTOAOC1' , LocaleSidKey='en_US', ProfileId = objP.Id , TimeZoneSidKey='America/Los_Angeles', LanguageLocaleKey='en_US' ); insert objU1; User objU2 = new User( Alias = 'TTOAOC2', Email='TTOAOC2@testorg.com' , CommunityNickName = 'TTOAOC2', EmailEncodingKey='UTF-8' , UserName='TestingTTOAOC2@testorg.com', LastName = 'TTOAOC2' , LocaleSidKey='en_US', ProfileId = objP.Id , TimeZoneSidKey='America/Los_Angeles', LanguageLocaleKey='en_US' ); insert objU2; // create account Account AccountToTest; system.runAs( objU1 ) { AccountToTest = new Account( Name = 'TestingAccountOwnerChange' ); insert AccountToTest; } // change owner of account system.runAs( objU2 ) { AccountToTest.OwnerId = objU2.Id; update AccountToTest; } // test the previous owner field AccountToTest = [ SELECT Id, Previous_Owner__C FROM Account WHERE Id = :AccountToTest.id ]; system.assertEquals( objU1.Id, AccountToTest.Previous_Owner__c, 'Previous Owner Test : ' + AccountToTest.Previous_Owner__c ); } }
Comments
8 responses to “Getting name of previous owner of account”
Hi Fernando,
great post as I’m trying to get the previous name on our records, too.
I have no experience in the trigger section at all tho, how do i create them to insert your formula and link them to the field?
Hi Tanja,
In order to create a trigger, you have to login to your Sandbox, go to the Setup menu, and navigate the left side bar to the option App Setup / Customize / Contacts / Triggers (that is, assuming you want to create the trigger in Contacts and that you have already created the lookup field).
Then click New and it will prompt with the trigger template that you can change:
trigger *name* on Contact (*events*) {
}
Once you change the code and are happy with the results, you can create a change set to send the trigger to production (menu Setup, option App Setup / Deploy / Outbound Change Sets, then create a change set including the trigger).
Then (it may take a while for the change set to be uploaded to Production) login to the Production org and navigate to Setup / App Setup / Deploy / Inbound Change Sets to get the change set deployed in Production.
Hi Fernando,
Thanks so much for the fast and helpful reply – thats great.
Will do this now.
Have a great day,
Best
Tanja
*From:* Learn SalesForce [mailto:comment-reply@wordpress.com] *Sent:* Tuesday, 16 October 2012 2:28 AM *To:* tanja@scoopon.com.au *Subject:* [New comment] Getting name of previous owner of account
Fernando F commented: “Hi Tanja, In order to create a trigger, you have to login to your Sandbox, go to the Setup menu, and navigate the left side bar to the option App Setup / Customize / Contacts / Triggers (that is, assuming you want to create the trigger in Contacts). The”
Hi Fernando,
sorry to bother you again….
I did as you instructed but my deployment in production always failed. Now I know i have to create a test class first and a test code to see if the there’s enough coverage for deployment of this trigger into production.
I’m stuck in creating the test code.
Do you have the test code for this trigger as well?
Thanks a lot,
Tanja
Tanja,
I’ve change the article and included a Test class to cover the trigger.
The trigger itself has been changed as well so be sure to use the new version.
Love your work!
Hey, this would solve a challenge we’re having. Is there any chance that this trigger could result in hitting the various APEX governor limits?
Probably a dumb question, but very new to this!
Hi Bob,
It will not hit the Governor limits since it is a very small “before” trigger that doesn’t run queries.
If you hit the Governor limit it is very likely that there is a problem somewhere else and in some other trigger.