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
Sunny SohailSunny Sohail 

Need help on trigger that converts contact owners to the accounts owner

Hello,

Is it possible to create a trigger that would change specific contacts owners to the account owner? Currenty we have a "TEST" user account in salesforceand anytime a contact is owned by this "TEST"user we want to change the contact owner to the account owner. Is this possible? any assistance would be appreciated.
 
Best Answer chosen by Sunny Sohail
UC InnovationUC Innovation
Try using Contains instead of Equals for the operator?  I'm not sure if Process Builder recognizes the 15 digit or 18 digit SFDC IDs.

All Answers

Amit VaidyaAmit Vaidya
Hi Sunny,

This should be done by standard way but unfortunately this is in threshold yet. So currently it can be done only through code. For reference, please find below link provided by Greg to update the contacts and opportunities owners with Account owner.

http://www.interactiveties.com/blog/2010/apex-reassign-contacts.php#.VW8wDs9VhBc

Thank you,
Amit
Sunny SohailSunny Sohail
thanks amit,

how would you go about to add a condition that would only change the contact owner to the account owner IF the contact owner is a specific account? also i dont need the opportunity owner to move its only contact that i care about which lines can we remove?
Amit VaidyaAmit Vaidya
Hi Sunny,

You can just remove all the line related to opportunity and reference with opportunity variables. Please try below:
 
/*
	Created by: Greg Hacic
	Last Update: 28 January 2010 by Greg Hacic
	Questions?: greg@interactiveties.com
*/
trigger reassignRelatedContactsAndOpportunities on Account (after update) {
	try {
		Set<Id> accountIds = new Set<Id>(); //set for holding the Ids of all Accounts that have been assigned to new Owners
		Map<Id, String> oldOwnerIds = new Map<Id, String>(); //map for holding the old account ownerId
		Map<Id, String> newOwnerIds = new Map<Id, String>(); //map for holding the new account ownerId
		Contact[] contactUpdates = new Contact[0]; //Contact sObject to hold OwnerId updates
		
		for (Account a : Trigger.new) { //for all records
			if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId) {
				oldOwnerIds.put(a.Id, Trigger.oldMap.get(a.Id).OwnerId); //put the old OwnerId value in a map
				newOwnerIds.put(a.Id, a.OwnerId); //put the new OwnerId value in a map
				accountIds.add(a.Id); //add the Account Id to the set
			}
		}
		
		if (!accountIds.isEmpty()) { //if the accountIds Set is not empty
			for (Account act : [SELECT Id, (SELECT Id, OwnerId FROM Contacts) FROM Account WHERE Id in :accountIds]) { //SOQL to get Contacts 
				String newOwnerId = newOwnerIds.get(act.Id); //get the new OwnerId value for the account
				String oldOwnerId = oldOwnerIds.get(act.Id); //get the old OwnerId value for the account
				for (Contact c : act.Contacts) { //for all contacts
					if (c.OwnerId == oldOwnerId) { //if the contact is assigned to the old account Owner
						Contact updatedContact = new Contact(Id = c.Id, OwnerId = newOwnerId); //create a new Contact sObject
						contactUpdates.add(updatedContact); //add the contact to our List of updates
					}
				}
				
			}
			update contactUpdates; //update the Contacts
		}
	} catch(Exception e) { //catch errors
		System.Debug('reassignRelatedContactsAndOpportunities failure: '+e.getMessage()); //write error to the debug log
	}
}

Thanks,
Amit
UC InnovationUC Innovation
If you don't need to write a trigger (as a requirement), you should also be able to use the Process Builder for this.
Sunny SohailSunny Sohail
Hi Ken,

Thanks for the suggestion. I have attempted to create a process via the process builder however im not sure how to go about creating the "immediate action". If you look at the screenshot below I start with the contacts object (first screenshot), then I move to the criteria "Contact owner = CTS Migrated" which is basically contact owner = an id . The id is the id for the "CTS Migrated" user (second screenshot). Then in the immeidiate action im having a difficult time telling the builder to change the contact owner to the account owner (3rd screenshot). Do you know how to create the immediate action so that the contact owner will transfer to the account owner IF the contact owner is "CTS Migrated" ?

thank you

This capture shows the "Contact Box".

This capture shows the "Contact owner = CTS Migrated" diamond.
This capture shows the "Immediate Action" box.
UC InnovationUC Innovation
I think you are almost there.  In the last screen shot, you just need to choose the Owner ID field in the Field column.  Then, in the Type column, choose "Reference".  Last, in the Value column, choose Account ID -> Owner ID.
Sunny SohailSunny Sohail
Hi Ken,

i have tried that however when i test this dones not seem to work. i tried to chnaged the contact owner to "CTS migrated" and when i did the contact owner did not change to the account owner.. maybe some kind of limitation?

.User-added image
UC InnovationUC Innovation
Try using Contains instead of Equals for the operator?  I'm not sure if Process Builder recognizes the 15 digit or 18 digit SFDC IDs.
This was selected as the best answer
Sunny SohailSunny Sohail
Bingo! Thanks Ken for working with me on this. this works. I used "starts with" instead of "equals". I think it is actually picking up the 18 digit id has opposed to the 15.
UC InnovationUC Innovation
Glad that you got it working.  It's always better to use the built-in Salesforce features whenever we can.