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
JJamesJJames 

Adding members to AccountTeamMembers in Trigger

I have this trigger below to pull out 4 users from my object Territories (ASD_Sales_Rep__c, PSD_Sales_Rep__c, Service_Sales_Rep__c, Switch_Sales_Rep__c, Transmission_Sales_Rep__c) and need to add those users in the AccountTeamMembers for the account.  How do you add accountteammembers to an account? i dont see the account team members field for accounts so not sure where to place these.

trigger acctTeamUpdate on Account (after update, after insert) {
    List<Account> accUpdateList = new List<Account>();
    AccountTeamMember[] newmembers = new AccountTeamMember[]{};
    
    for (Account a : Trigger.new)
    {
       Account acc = new Account();
       
        Territory__c t;
    
        try {                                                    // get the Territory record for the territory specified in the Account Record
            t = [SELECT ID, Name, ASD_Sales_Rep__c, PSD_Sales_Rep__c, Service_Sales_Rep__c, Switch_Sales_Rep__c, Transmission_Sales_Rep__c
                FROM Territory__c
                WHERE ID=:a.Territory__c LIMIT 1];
        }
        catch(exception ex){ 
            System.debug(' Failed Territory Record Retrieval');
        }

        AccountTeamMember addSalesRep=new AccountTeamMember();
        
        addSalesRep.AccountId=a.id;
        addSalesRep.UserId=t.ASD_Sales_Rep__c.uid;
        newmembers.add(addSalesRep);
        
        acc.Id = a.Id;
        accUpdateList.add(acc);

    }
    
    try{
        update accUpdateList;
    }
    catch(exception ex)
            { 
                System.debug(' Failed Account Update');
            }
}
Best Answer chosen by JJames
Abhishek BansalAbhishek Bansal
Hi James,

The only possible reason for that error is any of the four fields having blank value.
But you have already checked that. 
To be sure i have modified the code and put the null checks init.
Please try with the below trigger code.
trigger acctTeamUpdate on Account (after update, after insert) {
	Set<Id> teritoryIds = new Set<Id>();
	for(Account acc : trigger.new){
		if(acc.Territory__c != null){
			teritoryIds.add(acc.Territory__c);
		}
	}
	Map<Id,Territory__c> mapOfTerritory = new Map<Id,Territory__c>([Select ID, Name, ASD_Sales_Rep__c, PSD_Sales_Rep__c, Service_Sales_Rep__c, Switch_Sales_Rep__c, Transmission_Sales_Rep__c
	                                                                FROM Territory__c
	                                                                WHERE ID In : teritoryIds]);
	
	List<AccountTeamMember> accMemberList = new List<AccountTeamMember>();
	
	forAccount acc : trigger.new){
		if(mapOfTerritory.containssKey(acc.Territory__c)){
			if(mapOfTerritory.get(acc.Territory__c).ASD_Sales_Rep__c) != null)
				accMemberList.add(new AccountTeamMember(AccountId = acc.id, UserId = mapOfTerritory.get(acc.Territory__c).ASD_Sales_Rep__c));
			
			if(mapOfTerritory.get(acc.Territory__c).PSD_Sales_Rep__c) != null)
				accMemberList.add(new AccountTeamMember(AccountId = acc.id, UserId = mapOfTerritory.get(acc.Territory__c).PSD_Sales_Rep__c));
			
			if(mapOfTerritory.get(acc.Territory__c).Service_Sales_Rep__c) != null)
				accMemberList.add(new AccountTeamMember(AccountId = acc.id, UserId = mapOfTerritory.get(acc.Territory__c).Service_Sales_Rep__c));
			
			if(mapOfTerritory.get(acc.Territory__c).Transmission_Sales_Rep__c) != null)
				accMemberList.add(new AccountTeamMember(AccountId = acc.id, UserId = mapOfTerritory.get(acc.Territory__c).Transmission_Sales_Rep__c));
		}
	}
	if(accMemberList.size() > 0){
		insert accMemberList;
	}
}
Let me know if you still get the error or not.

Thanks,
Abhishek

All Answers

Abhishek BansalAbhishek Bansal
Hi James,

I saw your trigger code and found that there are lots of things which need to be corrected.
I also figured out what you want to achieve so i have written a trigger as per your requirement.
Please find the trigger code below :
trigger acctTeamUpdate on Account (after update, after insert) {
	Set<Id> teritoryIds = new Set<Id>();
	for(Account acc : trigger.new){
		if(acc.Territory__c != null){
			teritoryIds.add(acc.Territory__c);
		}
	}
	Map<Id,Territory__c> mapOfTerritory = new Map<Id,Territory__c>([Select ID, Name, ASD_Sales_Rep__c, PSD_Sales_Rep__c, Service_Sales_Rep__c, Switch_Sales_Rep__c, Transmission_Sales_Rep__c
	                                                                FROM Territory__c
	                                                                WHERE ID In : teritoryIds]);
	
	List<AccountTeamMember> accMemberList = new List<AccountTeamMember>();
	
	forAccount acc : trigger.new){
		if(mapOfTerritory.containssKey(acc.Territory__c)){
			accMemberList.add(new AccountTeamMember(AccountId = acc.id, UserId = mapOfTerritory.get(acc.Territory__c).ASD_Sales_Rep__c));
			accMemberList.add(new AccountTeamMember(AccountId = acc.id, UserId = mapOfTerritory.get(acc.Territory__c).PSD_Sales_Rep__c));
			accMemberList.add(new AccountTeamMember(AccountId = acc.id, UserId = mapOfTerritory.get(acc.Territory__c).Service_Sales_Rep__c));
			accMemberList.add(new AccountTeamMember(AccountId = acc.id, UserId = mapOfTerritory.get(acc.Territory__c).Transmission_Sales_Rep__c));
		}
	}
	if(accMemberList.size() > 0){
		insert accMemberList;
	}
}

Please replace your trigger code with above and that will help you to acheive what you want.
Let me know if your requirements are other than this or if you have any issue in this.

Thanks,
Abhishek
JJamesJJames
Thanks this is a much better way to do this.

I am getting the error: Error:Apex trigger acctTeamUpdate caused an unexpected exception, contact your administrator: acctTeamUpdate: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 3; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [User]: [User]: Trigger.acctTeamUpdate: line 24, column 1

it says missing field but i checked and all the users are populated in the territory.  Any ideas?
Pramodh KumarPramodh Kumar
Hey James can you share your screen so that I can solve the problem. Pramodh.thammishetty@gmail is my gmail Tpramodhkumar is my Skype.
Abhishek BansalAbhishek Bansal
Hi James,

The only possible reason for that error is any of the four fields having blank value.
But you have already checked that. 
To be sure i have modified the code and put the null checks init.
Please try with the below trigger code.
trigger acctTeamUpdate on Account (after update, after insert) {
	Set<Id> teritoryIds = new Set<Id>();
	for(Account acc : trigger.new){
		if(acc.Territory__c != null){
			teritoryIds.add(acc.Territory__c);
		}
	}
	Map<Id,Territory__c> mapOfTerritory = new Map<Id,Territory__c>([Select ID, Name, ASD_Sales_Rep__c, PSD_Sales_Rep__c, Service_Sales_Rep__c, Switch_Sales_Rep__c, Transmission_Sales_Rep__c
	                                                                FROM Territory__c
	                                                                WHERE ID In : teritoryIds]);
	
	List<AccountTeamMember> accMemberList = new List<AccountTeamMember>();
	
	forAccount acc : trigger.new){
		if(mapOfTerritory.containssKey(acc.Territory__c)){
			if(mapOfTerritory.get(acc.Territory__c).ASD_Sales_Rep__c) != null)
				accMemberList.add(new AccountTeamMember(AccountId = acc.id, UserId = mapOfTerritory.get(acc.Territory__c).ASD_Sales_Rep__c));
			
			if(mapOfTerritory.get(acc.Territory__c).PSD_Sales_Rep__c) != null)
				accMemberList.add(new AccountTeamMember(AccountId = acc.id, UserId = mapOfTerritory.get(acc.Territory__c).PSD_Sales_Rep__c));
			
			if(mapOfTerritory.get(acc.Territory__c).Service_Sales_Rep__c) != null)
				accMemberList.add(new AccountTeamMember(AccountId = acc.id, UserId = mapOfTerritory.get(acc.Territory__c).Service_Sales_Rep__c));
			
			if(mapOfTerritory.get(acc.Territory__c).Transmission_Sales_Rep__c) != null)
				accMemberList.add(new AccountTeamMember(AccountId = acc.id, UserId = mapOfTerritory.get(acc.Territory__c).Transmission_Sales_Rep__c));
		}
	}
	if(accMemberList.size() > 0){
		insert accMemberList;
	}
}
Let me know if you still get the error or not.

Thanks,
Abhishek
This was selected as the best answer
JJamesJJames
Worked, thank you!
JJamesJJames
Would it be easy to clear out the accountteammember list before updating the names? how can i delete them first?