{"id":29,"date":"2011-05-10T23:42:48","date_gmt":"2011-05-10T18:42:48","guid":{"rendered":"http:\/\/learnsf.wordpress.com\/?p=29"},"modified":"2011-05-10T23:42:48","modified_gmt":"2011-05-10T18:42:48","slug":"getting-name-of-previous-owner-of-account","status":"publish","type":"post","link":"https:\/\/blog.felineflock.com\/index.php\/2011\/05\/10\/getting-name-of-previous-owner-of-account\/","title":{"rendered":"Getting name of previous owner of account"},"content":{"rendered":"<p>1) Create a lookup field &#8220;Previous Owner&#8221; in Account as LookUp(<strong>User<\/strong>)<\/p>\n<p>2) Create a trigger like this:<\/p>\n<pre>trigger OnAccountOwnerChanged on Account (<strong>before update<\/strong>) {\n    for( Account a : Trigger.new ) {\n        Account AcctBeforeUpdate = Trigger.oldMap.get( a.Id );\n\n        if( AcctBeforeUpdate.OwnerId != a.OwnerId\n                <strong>&amp;&amp; ((String) a.OwnerId).startsWith( '005' ) <\/strong>)\n            a.Previous_Owner__c = AcctBeforeUpdate.OwnerId;\n    }\n}<\/pre>\n<p>3) Use the &#8220;Previous Owner&#8221; in your reports and formulas. It will let you reference the user name since it is a lookup field (eg.: Previous_Owner__r.LastName + &#8216;, &#8216; + Previous_Owner__r.FirstName).<\/p>\n<p>4) The part that says <\/p>\n<pre>&amp;&amp; ((String) a.OwnerId).startsWith( '005' ) <\/pre>\n<p> is for when the owner changes from one user to a queue. It will ignore queues as owners when setting the previous owner..<\/p>\n<p>5) Create a test class like this:<\/p>\n<pre>public with sharing class TestTriggerOnAccountOwnerChanged {\n    public static TestMethod void TestEverything() {\n        \/\/ get default profile\n        Profile objP = [ SELECT Id FROM Profile WHERE Name='Standard User' ]; \n\n        \/\/ create 2 users\n        User objU1 = new User( Alias = 'TTOAOC1', Email='TTOAOC1@testorg.com' \n                        , CommunityNickName = 'TTOAOC1', EmailEncodingKey='UTF-8'\n                        , UserName='TestingTTOAOC1@testorg.com', LastName = 'TTOAOC1'\n                        , LocaleSidKey='en_US', ProfileId = objP.Id\n                        , TimeZoneSidKey='America\/Los_Angeles', LanguageLocaleKey='en_US' );\n        insert objU1;\n        User objU2 = new User( Alias = 'TTOAOC2', Email='TTOAOC2@testorg.com' \n                        , CommunityNickName = 'TTOAOC2', EmailEncodingKey='UTF-8'\n                        , UserName='TestingTTOAOC2@testorg.com', LastName = 'TTOAOC2'\n                        , LocaleSidKey='en_US', ProfileId = objP.Id\n                        , TimeZoneSidKey='America\/Los_Angeles', LanguageLocaleKey='en_US' );\n        insert objU2;\n\n        \/\/ create account\n        Account AccountToTest;\n        system.runAs( objU1 ) {\n            AccountToTest = new Account( Name = 'TestingAccountOwnerChange' );\n            insert AccountToTest;\n        }\n\n        \/\/ change owner of account\n        system.runAs( objU2 ) {\n            AccountToTest.OwnerId = objU2.Id;\n            update AccountToTest;\n        }\n\n        \/\/ test the previous owner field\n        AccountToTest = [ SELECT Id, Previous_Owner__C FROM Account WHERE Id = :AccountToTest.id ];\n        system.assertEquals( objU1.Id, AccountToTest.Previous_Owner__c, 'Previous Owner Test : ' + AccountToTest.Previous_Owner__c );\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1) Create a lookup field &#8220;Previous Owner&#8221; 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 &amp;&amp; ((String) a.OwnerId).startsWith( &#8216;005&#8217; ) ) a.Previous_Owner__c = AcctBeforeUpdate.OwnerId; } } 3) Use the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[17],"tags":[],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-triggers"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":0,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}