I'm trying to create a trigger that fires and updates the Account/Opportunity Team Members when they are created using the Default Account/Opportunity Teams. However, my trigger does not seem to fire when the default Account/Opportunity Team Members are created using the standard default account/opportunity team functionality. Is there something I'm missing as far as when those records are created?
Trigger:
trigger AccountTeamMemberTrigger on AccountTeamMember (before insert, before update) {
if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)){
System.debug('Call Helper Class');
AccountTeamMemberTriggerHelper.updateTeamRole(trigger.new);
}
}
Helper:
public without sharing class AccountTeamMemberTriggerHelper {
//Method checks the Team Member Role to make sure it's the same as the User Role
//If not it will update the Team Member Role to match the User Role
public static void updateTeamRole(List<AccountTeamMember> accTeamMembers){
Set<Id> userSet = new Set<Id>();
for(AccountTeamMember atm: accTeamMembers){
System.debug('Add User to Set: ' + atm.UserId);
userSet.add(atm.UserId);
}
Map<Id,User> memberMap = new Map<Id,User>([SELECT Id, UserRole.Name FROM User where id IN: userSet]);
for(AccountTeamMember atm: accTeamMembers){
if(atm.TeamMemberRole != memberMap.get(atm.UserId).UserRole.Name)
atm.TeamMemberRole = memberMap.get(atm.UserId).UserRole.Name;
}
}
}
Thank you, Dan