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
Eager-2-LearnEager-2-Learn 

Contact Ownership Changes?

Hi,

 

This initial quesiton really dosn't have anything to do with Apex but if the answer is no then it will!

 

Is there a built in feature that allows the owner of contacts associated with an account to change with the owner of the acount?  I do not see that as an option in the change owner screen on accounts.  The issue is if a person leaves a department or company and the account is transfered to someone else then later they want to update the contacts associated with the account they can't because the previous employee owned the contacts!

 

So if there isn't a built in SFDC feature I am thinking of a VF page that has a look up field to all active users.  The admin selects a user then all that users contacts are pulled onto the list screen with checkbox's next to them.  The admin can select all or specific users then another pick list to select who will be the new owner is selected and finally a Transfer button to do the Apex work of changing the owner.

 

Now my apex / vf question(s).  At a higlevel I am thnking that I can have a top section with the two look up fields (FROM OWNER and TO OWNER) lets.  Next to the FROM OWNER I will have a button named Get Contacts and next to the TO OWNER I will have a button named Transfer..

 

Can I build a controller that I can use in a VF page as an extension and use the built in SFDC List capability that has the checkbox features?  If so can someone guide me a few examples hwo that is used.  I had one months ago but cannot seem to find it.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
AVictorioAVictorio

Tom,

 

You can create an apex trigger that will update the child contacts of an account whenever the account ownerid field is updated.

 

Here's a trigger that might work for you:

 

trigger UpdateContactOwnerFromAccount on Account (after update) {
	
	map<Id,Id> AccOwnerMap = new map<Id,Id>();
	
	for(Account a : trigger.new) {
		if(a.OwnerId != trigger.oldMap.get(a.Id).OwnerId) {
			AccOwnerMap.put(a.Id,a.OwnerId);
		}
	}
	
	list<Contact> ContactsToUpdate = new list<Contact>();
	
	for(Contact c: [select Id, OwnerId, AccountId 
	from Contact where AccountId IN :AccOwnerMap.keySet()]) {
		if(AccOwnerMap.containsKey(c.AccountId)) {
			Id AccOwnerId = AccOwnerMap.get(c.AccountId);
			c.OwnerId = AccOwnerId;
			ContactsToUpdate.add(c);
		}
	}
	
	if(ContactsToUpdate.size() > 0) {
		update ContactsToUpdate;
	}

}

 

All Answers

AmitSahuAmitSahu

Yes, you can create an extension and use it in VF page with the Edit and Delete options.

 

Use <apex:listViews>, this will have checkboxes in the list view.

AVictorioAVictorio

Tom,

 

You can create an apex trigger that will update the child contacts of an account whenever the account ownerid field is updated.

 

Here's a trigger that might work for you:

 

trigger UpdateContactOwnerFromAccount on Account (after update) {
	
	map<Id,Id> AccOwnerMap = new map<Id,Id>();
	
	for(Account a : trigger.new) {
		if(a.OwnerId != trigger.oldMap.get(a.Id).OwnerId) {
			AccOwnerMap.put(a.Id,a.OwnerId);
		}
	}
	
	list<Contact> ContactsToUpdate = new list<Contact>();
	
	for(Contact c: [select Id, OwnerId, AccountId 
	from Contact where AccountId IN :AccOwnerMap.keySet()]) {
		if(AccOwnerMap.containsKey(c.AccountId)) {
			Id AccOwnerId = AccOwnerMap.get(c.AccountId);
			c.OwnerId = AccOwnerId;
			ContactsToUpdate.add(c);
		}
	}
	
	if(ContactsToUpdate.size() > 0) {
		update ContactsToUpdate;
	}

}

 

This was selected as the best answer
Eager-2-LearnEager-2-Learn

Hi Avictorio,

 

That will work perfectly for moving forward for future account owner changes.  I was in bed lastnight thinking how can I prevent this issue from occuring and this morning my prayer was answered.

 

For issues with thousands of contacts where the owner still needs to be changed I am thinking to create a basic VF page that has two look up fields as I previously described.  This could be dangerous but since the admin will be the only one with access to it I think we will be ok.  And for auditing I am thinking that I send the list of contacts that get changed via email to the admin.

 

Thank you again.

darcusmietzdarcusmietz
Tom - Could you perhaps share your Apex Text Class for this Trigger with me? I've having a rough time figuring out how to run a test to cover the AccOwnerMap.

- Marcus
Eager-2-LearnEager-2-Learn

Hi Marcus,

 

Unfortunately, I never actually use the code snippet that was provided above  Went in different direction and never used a trigger.  Used a controller and a VF page that allows an admin to select a user in the Current Owner combo box and an user from the New Owner combo box and a Change Owner button that ties to a controller that does all the work of changing every contact from one owner to the other.