+ Start a Discussion
Vijay Zutshi 2Vijay Zutshi 2 

Multiple Account Member not getting added to Account Team

I have a trigger to create Cutomer Record for the Account Record. When this happens the user in Account Manager field will be automatically added to Account Team of that associated Account. The trigger is just creating 1 team member. So when I want to add another one it does not get added to the Team Member. My trigger is as follows:-

trigger customerAccount on Customer__c (after insert) {
    if(Trigger.IsAfter && Trigger.IsInsert) {
        SET<ID> customerId = new SET<ID>();
        SET<ID> accountManager = new SET<ID>();
        LIST<Account> newAccount = new LIST<Account>();
        LIST<AccountTeamMember> teamAccount = new LIST<AccountTeamMember>();
        MAP<ID, Account> mapIdAccount = new MAP<ID, Account>();
        MAP<ID, User> mapIdUser = new MAP<ID, User>();
        for(Customer__c newCustomer : Trigger.new) {
        LIST<Account> searchAcc = [SELECT Id, Name
                                   FROM Account
                                   WHERE ID IN :customerId];
        for(Account newAccountSearch :searchAcc) {
         mapIdAccount.put(newAccountSearch.Id, newAccountSearch);   
        LIST<User> searchUser = [SELECT Id, name,AccountId
                                 FROM User
                                 WHERE Id IN :accountManager];
        for(User addUser :searchUser) {
          mapIdUser.put(addUser.Id, addUser);  
        //for(Account addTeam :searchAcc) {            
        //for(User adduser1 :searchUser) {
        for(Customer__c addCustomerAcc : Trigger.new) {
            if(addCustomerAcc.Account_Manager__c != NULL) {
            accountTeamMember team = new accountTeamMember();
            team.AccountAccessLevel = 'Edit';
            team.AccountId = addCustomerAcc.Account__c;
            team.UserId = addCustomerAcc.Account_Manager__c;
            team.TeamMemberRole = 'Account Manager';    
        //team.Id = mapIdUser.get(adduser1.Id).AccountId;
        //team.UserId = Trigger.new[0].Id;
        //team.AccountId = mapIdUser.get(addteam.Name).Id; 
        if(teamAccount != NULL && teamAccount.size()>0) {         
        insert teamAccount;   
        //team.UserId = mapIdUser.get(addTeam.OwnerId);
        //team.AccountId = mapIdUser.get(addTeam.Id).Name;         

Please advise where I am going wrong.

Vijay Zutshi
AnudeepAnudeep (Salesforce Developers) 
Hi Vijay, 

Can you try adding a account team member manually on the specified account with the same values given in your trigger?

Also, Can you please add a system debug before the line insert teamAccount; to check the how many records are there to insert. I also suggest by trying replicate the same with a simple trigger. may be you can try creating a simple trigger on Account to see if you are able to add multiple accountteammembers
trigger AccountTeamTrig on Account (after insert) {
	List<AccountTeamMember> teams=new List<AccountTeamMember>();
    User u=[select id from User where alias='test'];
    for(Account a:Trigger.New){
        if(a.Industry=='Education' && a.AnnualRevenue > 500000){
            AccountTeamMember atm=new AccountTeamMember();
            atm.TeamMemberRole='Account Manager';
    insert teams;

Let me know if it helps