+ Start a Discussion
DaNae PetersonDaNae Peterson 

Need help removing account team members on child accounts

Hello all, 

In my org, we utilize account hierarchy and I am currently writing a trigger that changes made on the parent account (7 specific fields) should be reflected on all the child accounts (there can be many child accounts to one parent).  Where I am getting stuck is on the account team.  If I change the owner (one of the 7 fields) on the PARENT account the account team changes to the default of the new owner.  However, when the trigger fires all of the CHILD accounts list the new account team (from default) BUT also keeps old account team members.  I am trying to delete old account team members first (one specific role) but every time I test, it does not work. 

Perhaps someone else knows or notices something that I am missing?  Thank you!



trigger UpdateChildAccount on Account (after update){


map<id,Account> owner_change = new map<id,Account>();

List<Account> changeowneraccts = new List<Account>();
for(Account c : trigger.new){
if(trigger.oldmap.get(c.id).OwnerId != c.OwnerId){
owner_change.put(c.id,c);
}
}
List<AccountTeamMember> removeTeam = new List <AccountTeamMember>();
for(AccountTeamMember Ratm : [SELECT Id, UserId, AccountId FROM AccountTeamMember WHERE TeamMemberRole = 'Sales Support' AND AccountId IN :changeowneraccts]){
    if(removeTeam <> NULL && owner_change.size() > 0){
    removeTeam.add(Ratm);
}
}
delete removeTeam;


map<id,Account> acctId_to_acct = new map<id,Account>();
List<Account> updatedchildaccounts = new List<Account>();
for(Account a : trigger.new){
if(trigger.oldmap.get(a.id).OwnerId != a.OwnerId || trigger.oldmap.get(a.id).salesReach__Agent_Account_Company_Name__c != a.salesReach__Agent_Account_Company_Name__c || trigger.oldmap.get(a.id).salesReach__Agent_Contact_Name__c != a.salesReach__Agent_Contact_Name__c || trigger.oldmap.get(a.id).Diamond_Account__c != a.Diamond_Account__c || trigger.oldmap.get(a.id).Net_One__c != a.Net_One__c || trigger.oldmap.get(a.id).Account_Status__c != a.Account_Status__c || trigger.oldmap.get(a.id).Affinity__c != a.Affinity__c){
acctId_to_acct.put(a.id,a);
}
}
List<Account> childAccounts = [select ParentId, OwnerId, salesReach__Agent_Account_Company_Name__c, salesReach__Agent_Contact_Name__c, Diamond_Account__c, Net_One__c, Account_Status__c, Affinity__c from Account where ParentId in :acctId_to_acct.keyset()];
if(childAccounts <> NULL && acctId_to_acct.size() > 0){
for(Account c : childAccounts){
c.Ownerid = acctId_to_acct.get(c.parentid).Ownerid;
c.salesReach__Agent_Account_Company_Name__c = acctId_to_acct.get(c.parentid).salesReach__Agent_Account_Company_Name__c;
c.salesReach__Agent_Contact_Name__c = acctId_to_acct.get(c.parentid).salesReach__Agent_Contact_Name__c;
c.Diamond_Account__c = acctId_to_acct.get(c.parentid).Diamond_Account__c;
c.Net_One__c = acctId_to_acct.get(c.parentid).Net_One__c;
c.Account_Status__c = acctId_to_acct.get(c.parentid).Account_Status__c;
c.Affinity__c = acctId_to_acct.get(c.parentid).Affinity__c;    
updatedchildaccounts.add(c);
}       
update updatedchildaccounts;
}
}
Best Answer chosen by DaNae Peterson
RamuRamu (Salesforce Developers) 
Hi,

I reviewed your code and mostly the first part of your trigger code that is written to delete accoun team members from the subsidary accounts. I added few comments beside the lines that would need further tweaking hope this might help you fix the issue

map<id,Account> owner_change = new map<id,Account>();

List<Account> changeowneraccts = new List<Account>(); // I guess you missed writing an SOQL query to get all the child accounts that has the parent account in trigger.new collection. You would need to first get all the parent id's from trigger.new collection and write a SOQL query to get all associated child account records.

for(Account c : trigger.new){
if(trigger.oldmap.get(c.id).OwnerId != c.OwnerId){
owner_change.put(c.id,c); //This cosists of the account records of Parent accounts only and does not include child account records that are associated to these parent accounts. You would need to use 'changeowneraccts' list instead of Trigger.new

}
}
List<AccountTeamMember> removeTeam = new List <AccountTeamMember>();
for(AccountTeamMember Ratm : [SELECT Id, UserId, AccountId FROM AccountTeamMember WHERE TeamMemberRole = 'Sales Support' AND AccountId IN :changeowneraccts]){   // changeonweraccts is null currently as it is an empty list as per your code above.

    if(removeTeam <> NULL && owner_change.size() > 0){
    removeTeam.add(Ratm);
}
}
delete removeTeam;