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
Adrian-EAdrian-E 

Apex trigger to copy from Parent to Child account

Anyone know how to write an apex trigger that will copy the parent account team details to all child account teams everytime a change is made to the parent?
Best Answer chosen by Admin (Salesforce Developers) 
WhyserWhyser

I don't have Account Team Members enabled for my account, so I'm only stabbing in the dark here... 

Bear in mind, this may not be the most efficient code, I'm sure this code can be done much better. I don't even do any try/catch statements

 

I have not checked whether this code works or not, so you're gonna have to let me know whether it works or not.

 

trigger AccountTeamMemberTrigger on AccountTeamMember( after insert, after update, after delete ) { List< AccountTeamMember > insertAtm = new List< AccountTeamMember >(); List< AccountTeamMember > updateAtm = new List< AccountTeamMember >(); List< AccountTeamMember > deleteAtm = new List< AccountTeamMember >(); for ( AccountTeamMember atm : Trigger.new ) { // Determine if the Account is a parent List< AccountTeamMembers > atmList = [select AccountId, TeamMemberRole, UserId, AccountAccessLevel from AccountTeamMember where isDeleted = false and Account.ParentId = :atm.AccountId]; List< Account > aList = [select id from Account where Account.ParentId = :atm.AccountId]; // If the list is empty, there are no parent accounts if ( aList.size() > 0 ) { if ( Trigger.isInsert ) { // Assuming child Accounts don't have what the parents do, just add them for ( Account a : aList ) { AccountTeamMember atmInsert = atm.clone( false, true ); atmInsert.AccountId = a.Id; insertAtm.add( atmInsert ); } } if ( Trigger.isUpdate ) { // If the team member UserId matches, update that team member for ( AccountTeamMember atmUpdate : atmList ) { if ( atmUpdate.UserId == atm.UserId ) { atmUpdate.TeamMemberRole = atm.TeamMemberRole; atmUpdate.AccountAccessLevel = atm.AccountAccessLevel; updateAtm.add( atmUpdate ); } } } if ( Trigger.isDelete ) { // If the team member UserId matches, delete that team member for ( AccountTeamMember atmDelete : atmList ) { if ( atmDelete.UserId == atm.UserId ) { atmDelete.TeamMemberRole = atm.TeamMemberRole; atmDelete.AccountAccessLevel = atm.AccountAccessLevel; deleteAtm.add( atmDelete); } } } } } // After all the processing is done, do the required insert/update/deletes if ( insertAtm.size() > 0 ) insert insertAtm; if ( updateAtm.size() > 0 ) update updateAtm; if ( deleteAtm.size() > 0 ) delete deleteAtm; }

 

Message Edited by Whyser on 02-26-2009 11:38 AM

All Answers

WhyserWhyser

I don't have Account Team Members enabled for my account, so I'm only stabbing in the dark here... 

Bear in mind, this may not be the most efficient code, I'm sure this code can be done much better. I don't even do any try/catch statements

 

I have not checked whether this code works or not, so you're gonna have to let me know whether it works or not.

 

trigger AccountTeamMemberTrigger on AccountTeamMember( after insert, after update, after delete ) { List< AccountTeamMember > insertAtm = new List< AccountTeamMember >(); List< AccountTeamMember > updateAtm = new List< AccountTeamMember >(); List< AccountTeamMember > deleteAtm = new List< AccountTeamMember >(); for ( AccountTeamMember atm : Trigger.new ) { // Determine if the Account is a parent List< AccountTeamMembers > atmList = [select AccountId, TeamMemberRole, UserId, AccountAccessLevel from AccountTeamMember where isDeleted = false and Account.ParentId = :atm.AccountId]; List< Account > aList = [select id from Account where Account.ParentId = :atm.AccountId]; // If the list is empty, there are no parent accounts if ( aList.size() > 0 ) { if ( Trigger.isInsert ) { // Assuming child Accounts don't have what the parents do, just add them for ( Account a : aList ) { AccountTeamMember atmInsert = atm.clone( false, true ); atmInsert.AccountId = a.Id; insertAtm.add( atmInsert ); } } if ( Trigger.isUpdate ) { // If the team member UserId matches, update that team member for ( AccountTeamMember atmUpdate : atmList ) { if ( atmUpdate.UserId == atm.UserId ) { atmUpdate.TeamMemberRole = atm.TeamMemberRole; atmUpdate.AccountAccessLevel = atm.AccountAccessLevel; updateAtm.add( atmUpdate ); } } } if ( Trigger.isDelete ) { // If the team member UserId matches, delete that team member for ( AccountTeamMember atmDelete : atmList ) { if ( atmDelete.UserId == atm.UserId ) { atmDelete.TeamMemberRole = atm.TeamMemberRole; atmDelete.AccountAccessLevel = atm.AccountAccessLevel; deleteAtm.add( atmDelete); } } } } } // After all the processing is done, do the required insert/update/deletes if ( insertAtm.size() > 0 ) insert insertAtm; if ( updateAtm.size() > 0 ) update updateAtm; if ( deleteAtm.size() > 0 ) delete deleteAtm; }

 

Message Edited by Whyser on 02-26-2009 11:38 AM
This was selected as the best answer
dmsx2oddmsx2od

That example code is a great start.  I would tweak it so that the trigger fires on the Account object, which would require some fancy relationship code work, but it seems feasible.

 

Apex Managed Sharing does not apply to standard objects (possibly yet - but Safe Harbor applies) so Apex is the only way to go.

SidMSidM

I know this is quite an old thread, but just to be clear - this approach is not currently possible (26.0) as AccountTeamMember does not support triggers. Also, AccountTeamMember updates do not cause a knock-on modification to any other objects from which a trigger or workflow can be hung off, as in the case of some master-detail relationships.

 

Currently, the only way to programatically detect changes in AccountTeamMember and trigger a downstream process would be to run a Scheduled Batch process that queries the AccountTeamMember object on a fairly regular frequency. It's certainly not real-time like a trigger, but it can still be a useful pattern for many use cases.