Ask Search:
Christopher Eikanger AndersenChristopher Eikanger Andersen 

Align Contact ownership with Account Ownership

Hello!

From time to time, we do mass import of Contacts in Salesforce from a third party system where they maintain their user data. When doing this, the person importing the Contacts gets the ownership on those.

Is there any way of automating a routine that, on creation, aligns the ownership of any Contact with the Account owner that they are assigned to?

Steve MolisSteve Molis
 How are you importing the Contact Data?
Christopher Eikanger AndersenChristopher Eikanger Andersen
Currently, I'm using the Import wizard in Salesforce. I know that it is possible to specify Contact ownership if using the Data Loader, but we do not have Account ownership information in the system I am exporting from.. Therefore, the matching needs to be done post-import in Salesforce..
Brad GrossBrad Gross
Interesting question

You may be able to do this by inserting a workflow rule but I'm worried that you will hit your Apex limits when you do an import. But its worth a try

If its always the same userID that is doing the import from the 3rd party system.

Create a workflow rule that changes the owner to the account owner when a new contact is created and the ownerID = the userID from the 3rd party system

I haven't tested this so my advice may be slightly off but its worth investigating.

Otherwise you will need to:
- do a dataloader export of all accounts in salesforce to get their owner IDs
- vlookup the account name on the import file with the account names in salesforce and then insert the ownerID as a new column for the import file

you can see that if you have a decent size database - excel is gonna choke on it and now you are bringing in a tool like MS Access to get the job done. Not very cloud-like now is it.. Also a major timesink.
Steve MolisSteve Molis
 I would go with either of @ImpStout's solutions
Jason PaquetteJason Paquette
If you regularly do a lot of bulk data updates (i.e. you use tools like import wizards and Dataloader more than "occasionally"), I would recommend CRMFusion DemandTools... WELL WORTH THE MONEY.  They have a more powerful/flexible version of Dataloader as well as tools that allow manipulation of the data without having to pull it out to XLS/CSV first.  I bring this up to answer your question because they have a tool for reassigning ownership.  I use it on a regular basis and one of the things I do periodically is use this tool to go back and make sure all Contacts are always owned by the Account owner (I could even schedule it to happen unattended periodically, but I'm a bit of a control freak :) ).  Takes just a few minutes to accomplish the task.  I also use it for reassigning all Leads/Accounts/Contacts when territory assignments change, etc.  Far quicker and easier than using the built-in SFDC tools, or exporting to CSV and using dataloader.

re: ImStout's suggestion about a WFR, you can't do a field update on Contact Owner to dynamically grab the Account Owner... I've tried to write this rule before and you can't get there.
Doug AyersDoug Ayers
Hi Christopher, I had this same need in our org.  More to do with aligning our sharing rules and such, but this was a basic trigger I wrote to force new and updated contacts to be owned by the parent account:

trigger reassignContactOwnerToAccountOwner on Contact ( before insert, before update ) {

    List<Id> accountIds = new List<Id>();
    Map<Id, Id> accountOwnerIdMap = new Map<Id, Id>();

    // all the accounts whose owner ids to look up
    for ( Contact c : Trigger.new ) {
        accountIds.add( c.accountId );
    }
    
    // look up each account owner id
    for ( Account acct : [ SELECT id, ownerId FROM account WHERE id IN :accountIds ] ) {
        accountOwnerIdMap.put( acct.id, acct.ownerId );
    }
    
    // change contact owner to its account owner
    for ( Contact c : Trigger.new ) {
        c.ownerId = accountOwnerIdMap.get( c.accountId );
    }
   
}
Jay KilianJay Kilian
As a follow up, does anyone have the code to update the Contact Owner to match the Account Owner every time an ACCOUNT is updated? It'd be like a trickle down affect -change the Account Owner and all the Contacts would be updated to match automagically without having to touch the Contact record.

We change Account Owners to represent sales ownership, and that can change semi-frequently.

Thanks in advance!
Frank ChangFrank Chang
I Second Jay's question.  Has anyone solved the need to have Contact Owner matching that of the Account Owner when an Account is updated?

Thanks,
Frank
Erik WiltingErik Wilting
Greg Hacic wrote a code that updates opportunity and contact owner when the account owner is changed. 

http://www.interactiveties.com/blog/2010/apex-reassign-contacts.php
Isaac LewisIsaac Lewis
A word of advice on the trigger suggested by Doug Ayers,

Make sure you check filter for null values potentially created by other triggers...

//Reassigns Contact to Account Owner

trigger reassignContact on Contact (before insert, before update) {
   try {

        Set<Id> accountIds = new Set<Id>();
        Map<Id, Id> accountOwnerIdMap = new Map<Id, Id>();
   
        // all the accounts whose owner ids to look up
        for ( Contact c : Trigger.new ) {
            if(c.accountId <> null){
             accountIds.add( c.accountId );
            }
        }
       
        // look up each account owner id
        for ( Account acct : [ SELECT id, ownerId FROM account WHERE id IN :accountIds ] ) {
            accountOwnerIdMap.put( acct.id, acct.ownerId );
        }
       
        // change contact owner to its account owner
        for ( Contact c : Trigger.new ) {
            if(c.AccountId <> null){
             c.ownerId = accountOwnerIdMap.get( c.accountId );
            }
        }
    } catch(Exception e) { //catch errors
        System.Debug('reassignContacts failure: '+e.getMessage()); //write error to the debug log
    }

}
Robert GreenRobert Green
Do you have test code for this trigger?
Fredrik WidarssonFredrik Widarsson

Had the same issue for many years as SFDC Admin for a large sales team. Leads, Accounts, Contacts and Opportunities were often misaligned due to updates from the ERP system, changes in territory borders and sales team members coming and going.

No matter how much we worked on realigning data, over 15% of our data was owned by the wrong person. This had a negative impact on trust in the CRM data, especially when commissions were paid to the wrong sales people.

Ended up solving the problem with an app. We're now seeing SFDC Admins using the app to keep all CRM data aligned, correcting thousands of records on the fly.

Please learn more here: http://www.realzips.com/blog/2015/3/10/need-to-align-ownership-across-salesforcecom

Isaac LewisIsaac Lewis
This won't work off the cuff, since we use a test data factory, but it gives the gist of what you'll need to test:
 
@isTest static void test_reassignContactOwner(){

        //Generate Test Data
        List<User> Users = TestUtilities.generateUsers(2);
        Insert Users;
        User u1 = Users[0];
	    User u2 = Users[1];
    
        Account acc = TestUtilities.generateAccount();
        acc.OwnerId = u1.Id;
        Insert acc;

        Contact ct = TestUtilities.generateContact(acc);
		ct.OwnerId = u2.Id;
        Insert ct;

	    //Assert Contact Owner = Account Owner
	    ct = [SELECT ID, OwnerId FROM Contact WHERE Id = :ct.Id];
	    System.assertEquals(u1.Id,ct.OwnerId);
	    System.assertEquals(acc.OwnerId,ct.OwnerId);

		//Update Contact Owner
        ct.OwnerId = u2.Id;
        Update ct;
        
	    //Assert Contact Owner = Account Owner
	    ct = [SELECT ID, OwnerId FROM Contact WHERE Id = :ct.Id];
	    System.assertEquals(u1.Id,ct.OwnerId);
	    System.assertEquals(acc.OwnerId,ct.OwnerId);
        
        
    }

 
Robert GreenRobert Green
Thank you so much for this!!!
Slava BilanSlava Bilan
Guys,

I haven't learned triggers yet but really need to auto-update owners, I'm wasting tons of time on reassigning manually.

I see 2 triggers above - one to change Contact Owner when Contact is updated or created, another - when Account is updated. What to do if I need both? :) Shall I create 2 triggers? Or I need someone to write one that will include both types of actions?

Thanks!
Tonya LeeTonya Lee

I also have need for this as well but not only for Contact list imports but any time our territories have been updated that are already in our system.

Is it possible to do this with Process Builder?
Thanks for any help.

Fredrik WidarssonFredrik Widarsson

Hi Tonya and Others!

Please have a look at our Territory Management tool RealZips.com. (http://www.realzips.com" target="_blank)

Our app assigns all Zip codes across your sales teams. From that point, you can very easily sync territories and ownership of Leads, Accounts, Contacts and Opps. You also get complete change history of reassignment of territories on a Zip code level (life-saver). RealZips is 100% native and works with standard Lead Assignment, Account Sharing, Process Builder and Territory Management 2.0.

Jacky GongJacky Gong
How did you solve the issue ?