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
JN22JN22 

Account Team Trigger

Hello,

 

I have a trigger off the Account object that is designed to update the Account Team.  Since I could not write a trigger based on the Account Team object, I created 3 custom fields at the Account level.  The trigger is meant to take the values from those fields and update the Account Team object appropriately.  The issue I am having is when I try to combine an addition/change with a deletion, or if I try to delete more than 1 value.  For example, if I have the 3 fields populated and I change the Industry_Manager__c field and remove the value from the Client_Advisor__c field, it changes the Industry_Manager__c value, but also deltes both the Client_Advisr__c and Market_Director__c values.  Likewise, if I delete 2 values, it will only remove 1 from the Account Team object.  Can anyone help as I am stumped how to fix this.  My trigger is below:

 

trigger AccountTeamChanges on Account(after insert, after update)
{
//list to hold new account team members
List<AccountTeamMember> acctMembers = new List<AccountTeamMember>();

//list to hold new account sharing rules
List<AccountShare> acctSharingRules = new List<AccountShare>();

//misc
Set<String> rmMemberAccts = new Set<String>();
Map<ID, List<ID>> AccountTeamMap = new Map<ID, List<ID>>();

//iterate through records to find update processor values
for(Account a : Trigger.new)
{

//new Account - Client Advisor
if(Trigger.isInsert && a.Client_Advisor__c != null)
{
AccountTeamMember ca = new AccountTeamMember();
ca.AccountId = a.Id;
ca.TeamMemberRole = 'Client Advisor';
ca.UserId = a.Client_Advisor__c;
acctMembers.add(ca);

AccountShare caSharingRule = new AccountShare();
caSharingRule.AccountId = a.Id;
caSharingRule.OpportunityAccessLevel = 'Read';
caSharingRule.CaseAccessLevel = 'Read';
caSharingRule.AccountAccessLevel = 'Edit';
caSharingRule.UserOrGroupId = a.Client_Advisor__c;
acctSharingRules.add(caSharingRule);
}

//new Account - Market Director
if(Trigger.isInsert && a.Market_Director__c != null)
{
AccountTeamMember md = new AccountTeamMember();
md.AccountId = a.Id;
md.TeamMemberRole = 'Market Director';
md.UserId = a.Market_Director__c;
acctMembers.add(md);

AccountShare mdSharingRule = new AccountShare();
mdSharingRule.AccountId = a.Id;
mdSharingRule.OpportunityAccessLevel = 'Read';
mdSharingRule.CaseAccessLevel = 'Read';
mdSharingRule.AccountAccessLevel = 'Edit';
mdSharingRule.UserOrGroupId = a.Market_Director__c;
acctSharingRules.add(mdSharingRule);
}

//new Account - Industry Manager
if(Trigger.isInsert && a.Industry_Manager__c != null)
{
AccountTeamMember im = new AccountTeamMember();
im.AccountId = a.Id;
im.TeamMemberRole = 'Industry Manager';
im.UserId = a.Industry_Manager__c;
acctMembers.add(im);

AccountShare imSharingRule = new AccountShare();
imSharingRule.AccountId = a.Id;
imSharingRule.OpportunityAccessLevel = 'Read';
imSharingRule.CaseAccessLevel = 'Read';
imSharingRule.AccountAccessLevel = 'Edit';
imSharingRule.UserOrGroupId = a.Industry_Manager__c;
acctSharingRules.add(imSharingRule);
}

//updated Account
else if(Trigger.isUpdate)
{
//old Accoount record
Account oldAcct = Trigger.oldMap.get(a.Id);

//check to see if the team values have changed and verifies the
//new values are not null

system.debug('client advisor '+ a.Client_Advisor__c +'old value '+ oldAcct.Client_Advisor__c);

if(oldAcct.Client_Advisor__c != a.Client_Advisor__c && a.Client_Advisor__c != null)
{
//add old Client Advisor to remove list if one exists

rmMemberAccts.add(oldAcct.Id);
if(AccountTeamMap.get(oldAcct.Id) == null){
AccountTeamMap.put(oldAcct.Id, new List<ID>{oldAcct.Client_Advisor__c});
}
else{
AccountTeamMap.get(oldAcct.Id).add(oldAcct.Client_Advisor__c);
}
system.debug('Account teammap 1 : '+ AccountTeamMap);


//add new processor to account team and update sharing rules
AccountTeamMember ca = new AccountTeamMember();
ca.AccountId = a.Id;
ca.TeamMemberRole = 'Client Advisor';
ca.UserId = a.Client_Advisor__c;
acctMembers.add(ca);

AccountShare caSharingRule = new AccountShare();
caSharingRule.AccountId = a.Id;
caSharingRule.OpportunityAccessLevel = 'Read';
caSharingRule.CaseAccessLevel = 'Read';
caSharingRule.AccountAccessLevel = 'Edit';
caSharingRule.UserOrGroupId = a.Client_Advisor__c;
acctSharingRules.add(caSharingRule);
}
else if(oldAcct.Client_Advisor__c != a.Client_Advisor__c && a.Client_Advisor__c == null)
{
rmMemberAccts.add(a.Id);
if(AccountTeamMap.get(oldAcct.Id) == null){
AccountTeamMap.put(oldAcct.Id, new List<ID>{oldAcct.Client_Advisor__c});}
else{
AccountTeamMap.get(oldAcct.Id).add(oldAcct.Client_Advisor__c);}

}


//check to see if the team values have changed and verifies the
//new values are not null
if(oldAcct.Market_Director__c != a.Market_Director__c && a.Market_Director__c != null)
{
//add old Client Advisor to remove list if one exists

rmMemberAccts.add(oldAcct.Id);
if(AccountTeamMap.get(oldAcct.Id) == null){
AccountTeamMap.put(oldAcct.Id, new List<ID>{oldAcct.Market_Director__c});}
else{
AccountTeamMap.get(oldAcct.Id).add(oldAcct.Market_Director__c);}
system.debug('Account teammap 2 : '+ AccountTeamMap);


//add new processor to account team and update sharing rules
AccountTeamMember md = new AccountTeamMember();
md.AccountId = a.Id;
md.TeamMemberRole = 'Market Director';
md.UserId = a.Market_Director__c;
acctMembers.add(md);

AccountShare mdSharingRule = new AccountShare();
mdSharingRule.AccountId = a.Id;
mdSharingRule.OpportunityAccessLevel = 'Read';
mdSharingRule.CaseAccessLevel = 'Read';
mdSharingRule.AccountAccessLevel = 'Edit';
mdSharingRule.UserOrGroupId = a.Market_Director__c;
acctSharingRules.add(mdSharingRule);
}
else if(oldAcct.Market_Director__c != a.Market_Director__c && a.Market_Director__c == null)
{

rmMemberAccts.add(a.Id);
if(AccountTeamMap.get(oldAcct.Id) == null){
AccountTeamMap.put(oldAcct.Id, new List<ID>{oldAcct.Market_Director__c});}
else{
AccountTeamMap.get(oldAcct.Id).add(oldAcct.Client_Advisor__c);}

}


//check to see if the team values have changed and verifies the
//new values are not null
if(oldAcct.Industry_Manager__c != a.Industry_Manager__c && a.Industry_Manager__c != null)
{
//add old Client Advisor to remove list if one exists
if(oldAcct.Industry_Manager__c != null)

rmMemberAccts.add(oldAcct.Id);
if(AccountTeamMap.get(oldAcct.Id) == null){
AccountTeamMap.put(oldAcct.Id, new List<ID>{oldAcct.Industry_Manager__c});}
else{
AccountTeamMap.get(oldAcct.Id).add(oldAcct.Industry_Manager__c);}
system.debug('Account teammap 2 : '+ AccountTeamMap);


//add new processor to account team and update sharing rules
AccountTeamMember im = new AccountTeamMember();
im.AccountId = a.Id;
im.TeamMemberRole = 'Industry Manager';
im.UserId = a.Industry_Manager__c;
acctMembers.add(im);

AccountShare imSharingRule = new AccountShare();
imSharingRule.AccountId = a.Id;
imSharingRule.OpportunityAccessLevel = 'Read';
imSharingRule.CaseAccessLevel = 'Read';
imSharingRule.AccountAccessLevel = 'Edit';
imSharingRule.UserOrGroupId = a.Industry_Manager__c;
acctSharingRules.add(imSharingRule);
}
else if(oldAcct.Industry_Manager__c != a.Industry_Manager__c && a.Industry_Manager__c == null)
{

rmMemberAccts.add(a.Id);
if(AccountTeamMap.get(oldAcct.Id) == null){
AccountTeamMap.put(oldAcct.Id, new List<ID>{oldAcct.Market_Director__c});}
else{
AccountTeamMap.get(oldAcct.Id).add(oldAcct.Client_Advisor__c);}


}

}

//DML OPERATIONS
//remove team members from account team if any exist

}

if(rmMemberAccts.size() > 0)
{
system.debug('rmMemberAccts '+ rmMemberAccts.size());

List<AccountTeamMember> removeTeam = new List<AccountTeamMember>();
for(AccountTeamMember atm : [SELECT Id, UserId, AccountId, TeamMemberRole
FROM AccountTeamMember
WHERE (TeamMemberRole='Client Advisor' OR TeamMemberRole='Industry Manager' OR TeamMemberRole='Market Director') AND AccountId IN :rmMemberAccts])
{
for(ID uid: AccountTeamMap.get(atm.AccountId)){
if(atm.UserId == uid)
removeTeam.add(atm);
}
}
system.debug('Remove Team '+ removeTeam);
delete removeTeam;


}

system.debug('-->ACCOUNT MEMBERS: ' + acctMembers);
//insert the new account team members if any exist
if(acctMembers.size() > 0)
upsert acctMembers; //LINE 100

//insert account sharing rules if any exist
if(acctSharingRules.size() > 0)
upsert acctSharingRules;

}

Best Answer chosen by Admin (Salesforce Developers) 
JN22JN22

For anyone wondering, SFDC Support helped me resolve this.  The final trigger is below:

 

trigger AccountTeamChanges on Account(after insert, after update) 
{
   //list to hold new account team members
     List<AccountTeamMember> acctMembers = new List<AccountTeamMember>();
     
     //list to hold new account sharing rules
     List<AccountShare> acctSharingRules = new List<AccountShare>();
     
     //misc
     Set<String> rmMemberAccts = new Set<String>();
     Map<ID, List<ID>> AccountTeamMap = new Map<ID, List<ID>>(); 
    
    List<Account> acc = new list<Account>();
    
     //iterate through records to find update processor values
     for(Account a : Trigger.new)
     {
        
        if(Trigger.isInsert)
        {
            //new Account - Client Advisor
            if(a.Client_Advisor__c != null){
            
            AccountTeamMember ca = new AccountTeamMember();
            ca.AccountId = a.Id;
            ca.TeamMemberRole = 'Client Advisor';
            ca.UserId = a.Client_Advisor__c;
            acctMembers.add(ca);
            
            AccountShare caSharingRule = new AccountShare();
            caSharingRule.AccountId = a.Id;
            caSharingRule.OpportunityAccessLevel = 'Read';
            caSharingRule.CaseAccessLevel = 'Read';
            caSharingRule.AccountAccessLevel = 'Edit';
            caSharingRule.UserOrGroupId = a.Client_Advisor__c;
            acctSharingRules.add(caSharingRule);
            }

            //new Account - Market Director
            if(a.Market_Director__c != null){
            
            AccountTeamMember md = new AccountTeamMember();
            md.AccountId = a.Id;
            md.TeamMemberRole = 'Market Director';
            md.UserId = a.Market_Director__c;
            acctMembers.add(md);
            
            AccountShare mdSharingRule = new AccountShare();
            mdSharingRule.AccountId = a.Id;
            mdSharingRule.OpportunityAccessLevel = 'Read';
            mdSharingRule.CaseAccessLevel = 'Read';
            mdSharingRule.AccountAccessLevel = 'Edit';
            mdSharingRule.UserOrGroupId = a.Market_Director__c;
            acctSharingRules.add(mdSharingRule);
            }

            //new Account - Industry Manager
            if(a.Industry_Manager__c != null){
            
            AccountTeamMember im = new AccountTeamMember();
            im.AccountId = a.Id;
            im.TeamMemberRole = 'Industry Manager';
            im.UserId = a.Industry_Manager__c;
            acctMembers.add(im);
            
            AccountShare imSharingRule = new AccountShare();
            imSharingRule.AccountId = a.Id;
            imSharingRule.OpportunityAccessLevel = 'Read';
            imSharingRule.CaseAccessLevel = 'Read';
            imSharingRule.AccountAccessLevel = 'Edit';
            imSharingRule.UserOrGroupId = a.Industry_Manager__c;
            acctSharingRules.add(imSharingRule);
            }
        }

        //updated Account
        else if(Trigger.isUpdate)
        {
            //old Accoount record
            Account oldAcct = Trigger.oldMap.get(a.Id);
            
            //check to see if the team values have changed and verifies the
            //new values are not null

            system.debug('client advisor '+ a.Client_Advisor__c +'old value '+ oldAcct.Client_Advisor__c);

            if(oldAcct.Client_Advisor__c != a.Client_Advisor__c && a.Client_Advisor__c != null)
            {
                //add old Client Advisor to remove list if one exists

                rmMemberAccts.add(oldAcct.Id);
                    if(AccountTeamMap.get(oldAcct.Id) == null){
                        AccountTeamMap.put(oldAcct.Id, new List<ID>{oldAcct.Client_Advisor__c});
                        }
                    else{
                        AccountTeamMap.get(oldAcct.Id).add(oldAcct.Client_Advisor__c);
                        }
                system.debug('Account teammap 1 : '+ AccountTeamMap);
                

                
                //add new processor to account team and update sharing rules
                AccountTeamMember ca = new AccountTeamMember();
                ca.AccountId = a.Id;
                ca.TeamMemberRole = 'Client Advisor';
                ca.UserId = a.Client_Advisor__c;
                acctMembers.add(ca);
                
                AccountShare caSharingRule = new AccountShare();
                caSharingRule.AccountId = a.Id;
                caSharingRule.OpportunityAccessLevel = 'Read';
                caSharingRule.CaseAccessLevel = 'Read';
                caSharingRule.AccountAccessLevel = 'Edit';
                caSharingRule.UserOrGroupId = a.Client_Advisor__c;
                acctSharingRules.add(caSharingRule);
            }
            else if(oldAcct.Client_Advisor__c != a.Client_Advisor__c && a.Client_Advisor__c == null)
            {
                rmMemberAccts.add(a.Id);
                    if(AccountTeamMap.get(oldAcct.Id) == null){
                        AccountTeamMap.put(oldAcct.Id, new List<ID>{oldAcct.Client_Advisor__c});}
                    else{
                AccountTeamMap.get(oldAcct.Id).add(oldAcct.Client_Advisor__c);}

            }

            
            //check to see if the team values have changed and verifies the
            //new values are not null
            if(oldAcct.Market_Director__c != a.Market_Director__c && a.Market_Director__c != null)
            {
                //add old Client Advisor to remove list if one exists

                rmMemberAccts.add(oldAcct.Id);
                    if(AccountTeamMap.get(oldAcct.Id) == null){
                        AccountTeamMap.put(oldAcct.Id, new List<ID>{oldAcct.Market_Director__c});}
                    else{
                        AccountTeamMap.get(oldAcct.Id).add(oldAcct.Market_Director__c);}
                system.debug('Account teammap 2 : '+ AccountTeamMap);

                
                //add new processor to account team and update sharing rules
                AccountTeamMember md = new AccountTeamMember();
                md.AccountId = a.Id;
                md.TeamMemberRole = 'Market Director';
                md.UserId = a.Market_Director__c;
                acctMembers.add(md);
                
                AccountShare mdSharingRule = new AccountShare();
                mdSharingRule.AccountId = a.Id;
                mdSharingRule.OpportunityAccessLevel = 'Read';
                mdSharingRule.CaseAccessLevel = 'Read';
                mdSharingRule.AccountAccessLevel = 'Edit';
                mdSharingRule.UserOrGroupId = a.Market_Director__c;
                acctSharingRules.add(mdSharingRule);
            }
            else if(oldAcct.Market_Director__c != a.Market_Director__c && a.Market_Director__c == null)
            {

                rmMemberAccts.add(a.Id);
                    if(AccountTeamMap.get(oldAcct.Id) == null){
                        AccountTeamMap.put(oldAcct.Id, new List<ID>{oldAcct.Market_Director__c});}
                    else{
                AccountTeamMap.get(oldAcct.Id).add(oldAcct.Client_Advisor__c);}

            }

            
            //check to see if the team values have changed and verifies the
            //new values are not null
            if(oldAcct.Industry_Manager__c != a.Industry_Manager__c && a.Industry_Manager__c != null)
            {
                //add old Client Advisor to remove list if one exists
                if(oldAcct.Industry_Manager__c != null)

                rmMemberAccts.add(oldAcct.Id);
                    if(AccountTeamMap.get(oldAcct.Id) == null){
                        AccountTeamMap.put(oldAcct.Id, new List<ID>{oldAcct.Industry_Manager__c});}
                    else{
                        AccountTeamMap.get(oldAcct.Id).add(oldAcct.Industry_Manager__c);}
                system.debug('Account teammap 2 : '+ AccountTeamMap);

                
                //add new processor to account team and update sharing rules
                AccountTeamMember im = new AccountTeamMember();
                im.AccountId = a.Id;
                im.TeamMemberRole = 'Industry Manager';
                im.UserId = a.Industry_Manager__c;
                acctMembers.add(im);
                
                AccountShare imSharingRule = new AccountShare();
                imSharingRule.AccountId = a.Id;
                imSharingRule.OpportunityAccessLevel = 'Read';
                imSharingRule.CaseAccessLevel = 'Read';
                imSharingRule.AccountAccessLevel = 'Edit';
                imSharingRule.UserOrGroupId = a.Industry_Manager__c;
                acctSharingRules.add(imSharingRule);
            }
            else if(oldAcct.Industry_Manager__c != a.Industry_Manager__c && a.Industry_Manager__c == null)
            {

                rmMemberAccts.add(a.Id);
                    if(AccountTeamMap.get(oldAcct.Id) == null){
                        AccountTeamMap.put(oldAcct.Id, new List<ID>{oldAcct.Market_Director__c});}
                    else{
                AccountTeamMap.get(oldAcct.Id).add(oldAcct.Client_Advisor__c);}


            }

        }
        
        //DML OPERATIONS
        //remove team members from account team if any exist
        
        acc.add(a);

    }

        system.debug('-->ACCOUNT MEMBERS: ' + acctMembers);
        //insert the new account team members if any exist
        if(acctMembers.size() > 0)
            upsert acctMembers; //LINE 100
        
        //insert account sharing rules if any exist
        if(acctSharingRules.size() > 0)
            upsert acctSharingRules;
            
        if (acc[0].Client_Advisor__c == null )
        {
            list<AccountTeamMember> atm1 = [SELECT Id, UserId, AccountId, TeamMemberRole
                                            FROM AccountTeamMember
                                            WHERE (TeamMemberRole='Client Advisor') AND AccountId =:acc[0].id];
            if (atm1.size() > 0)
            {delete atm1;}
        }
        if (acc[0].Industry_Manager__c == null )
        {
            list<AccountTeamMember> atm1 = [SELECT Id, UserId, AccountId, TeamMemberRole
                                            FROM AccountTeamMember
                                            WHERE (TeamMemberRole='Industry Manager') AND AccountId =:acc[0].id];
            if (atm1.size() > 0)
            {delete atm1;}
        }
        if (acc[0].Market_Director__c == null )
        {
            list<AccountTeamMember> atm1 = [SELECT Id, UserId, AccountId, TeamMemberRole
                                            FROM AccountTeamMember
                                            WHERE (TeamMemberRole='Market Director') AND AccountId =:acc[0].id];
            if (atm1.size() > 0)
            {delete atm1;}
        }    
}

All Answers

SForceBeWithYouSForceBeWithYou

I notice that you have possibly copied lines that duplicate the System.debug of "Account teammap 2"... is this possibly where you maybe copied code into a section you intended to make distinct as a "Account teammap 3"?  Would this be a good case to have 2 or 3 different map variables instead?

JN22JN22

Thanks.  That was just a typo in my debug.  I tried to use multipl maps, but was still gettig the same issue.

JN22JN22

For anyone wondering, SFDC Support helped me resolve this.  The final trigger is below:

 

trigger AccountTeamChanges on Account(after insert, after update) 
{
   //list to hold new account team members
     List<AccountTeamMember> acctMembers = new List<AccountTeamMember>();
     
     //list to hold new account sharing rules
     List<AccountShare> acctSharingRules = new List<AccountShare>();
     
     //misc
     Set<String> rmMemberAccts = new Set<String>();
     Map<ID, List<ID>> AccountTeamMap = new Map<ID, List<ID>>(); 
    
    List<Account> acc = new list<Account>();
    
     //iterate through records to find update processor values
     for(Account a : Trigger.new)
     {
        
        if(Trigger.isInsert)
        {
            //new Account - Client Advisor
            if(a.Client_Advisor__c != null){
            
            AccountTeamMember ca = new AccountTeamMember();
            ca.AccountId = a.Id;
            ca.TeamMemberRole = 'Client Advisor';
            ca.UserId = a.Client_Advisor__c;
            acctMembers.add(ca);
            
            AccountShare caSharingRule = new AccountShare();
            caSharingRule.AccountId = a.Id;
            caSharingRule.OpportunityAccessLevel = 'Read';
            caSharingRule.CaseAccessLevel = 'Read';
            caSharingRule.AccountAccessLevel = 'Edit';
            caSharingRule.UserOrGroupId = a.Client_Advisor__c;
            acctSharingRules.add(caSharingRule);
            }

            //new Account - Market Director
            if(a.Market_Director__c != null){
            
            AccountTeamMember md = new AccountTeamMember();
            md.AccountId = a.Id;
            md.TeamMemberRole = 'Market Director';
            md.UserId = a.Market_Director__c;
            acctMembers.add(md);
            
            AccountShare mdSharingRule = new AccountShare();
            mdSharingRule.AccountId = a.Id;
            mdSharingRule.OpportunityAccessLevel = 'Read';
            mdSharingRule.CaseAccessLevel = 'Read';
            mdSharingRule.AccountAccessLevel = 'Edit';
            mdSharingRule.UserOrGroupId = a.Market_Director__c;
            acctSharingRules.add(mdSharingRule);
            }

            //new Account - Industry Manager
            if(a.Industry_Manager__c != null){
            
            AccountTeamMember im = new AccountTeamMember();
            im.AccountId = a.Id;
            im.TeamMemberRole = 'Industry Manager';
            im.UserId = a.Industry_Manager__c;
            acctMembers.add(im);
            
            AccountShare imSharingRule = new AccountShare();
            imSharingRule.AccountId = a.Id;
            imSharingRule.OpportunityAccessLevel = 'Read';
            imSharingRule.CaseAccessLevel = 'Read';
            imSharingRule.AccountAccessLevel = 'Edit';
            imSharingRule.UserOrGroupId = a.Industry_Manager__c;
            acctSharingRules.add(imSharingRule);
            }
        }

        //updated Account
        else if(Trigger.isUpdate)
        {
            //old Accoount record
            Account oldAcct = Trigger.oldMap.get(a.Id);
            
            //check to see if the team values have changed and verifies the
            //new values are not null

            system.debug('client advisor '+ a.Client_Advisor__c +'old value '+ oldAcct.Client_Advisor__c);

            if(oldAcct.Client_Advisor__c != a.Client_Advisor__c && a.Client_Advisor__c != null)
            {
                //add old Client Advisor to remove list if one exists

                rmMemberAccts.add(oldAcct.Id);
                    if(AccountTeamMap.get(oldAcct.Id) == null){
                        AccountTeamMap.put(oldAcct.Id, new List<ID>{oldAcct.Client_Advisor__c});
                        }
                    else{
                        AccountTeamMap.get(oldAcct.Id).add(oldAcct.Client_Advisor__c);
                        }
                system.debug('Account teammap 1 : '+ AccountTeamMap);
                

                
                //add new processor to account team and update sharing rules
                AccountTeamMember ca = new AccountTeamMember();
                ca.AccountId = a.Id;
                ca.TeamMemberRole = 'Client Advisor';
                ca.UserId = a.Client_Advisor__c;
                acctMembers.add(ca);
                
                AccountShare caSharingRule = new AccountShare();
                caSharingRule.AccountId = a.Id;
                caSharingRule.OpportunityAccessLevel = 'Read';
                caSharingRule.CaseAccessLevel = 'Read';
                caSharingRule.AccountAccessLevel = 'Edit';
                caSharingRule.UserOrGroupId = a.Client_Advisor__c;
                acctSharingRules.add(caSharingRule);
            }
            else if(oldAcct.Client_Advisor__c != a.Client_Advisor__c && a.Client_Advisor__c == null)
            {
                rmMemberAccts.add(a.Id);
                    if(AccountTeamMap.get(oldAcct.Id) == null){
                        AccountTeamMap.put(oldAcct.Id, new List<ID>{oldAcct.Client_Advisor__c});}
                    else{
                AccountTeamMap.get(oldAcct.Id).add(oldAcct.Client_Advisor__c);}

            }

            
            //check to see if the team values have changed and verifies the
            //new values are not null
            if(oldAcct.Market_Director__c != a.Market_Director__c && a.Market_Director__c != null)
            {
                //add old Client Advisor to remove list if one exists

                rmMemberAccts.add(oldAcct.Id);
                    if(AccountTeamMap.get(oldAcct.Id) == null){
                        AccountTeamMap.put(oldAcct.Id, new List<ID>{oldAcct.Market_Director__c});}
                    else{
                        AccountTeamMap.get(oldAcct.Id).add(oldAcct.Market_Director__c);}
                system.debug('Account teammap 2 : '+ AccountTeamMap);

                
                //add new processor to account team and update sharing rules
                AccountTeamMember md = new AccountTeamMember();
                md.AccountId = a.Id;
                md.TeamMemberRole = 'Market Director';
                md.UserId = a.Market_Director__c;
                acctMembers.add(md);
                
                AccountShare mdSharingRule = new AccountShare();
                mdSharingRule.AccountId = a.Id;
                mdSharingRule.OpportunityAccessLevel = 'Read';
                mdSharingRule.CaseAccessLevel = 'Read';
                mdSharingRule.AccountAccessLevel = 'Edit';
                mdSharingRule.UserOrGroupId = a.Market_Director__c;
                acctSharingRules.add(mdSharingRule);
            }
            else if(oldAcct.Market_Director__c != a.Market_Director__c && a.Market_Director__c == null)
            {

                rmMemberAccts.add(a.Id);
                    if(AccountTeamMap.get(oldAcct.Id) == null){
                        AccountTeamMap.put(oldAcct.Id, new List<ID>{oldAcct.Market_Director__c});}
                    else{
                AccountTeamMap.get(oldAcct.Id).add(oldAcct.Client_Advisor__c);}

            }

            
            //check to see if the team values have changed and verifies the
            //new values are not null
            if(oldAcct.Industry_Manager__c != a.Industry_Manager__c && a.Industry_Manager__c != null)
            {
                //add old Client Advisor to remove list if one exists
                if(oldAcct.Industry_Manager__c != null)

                rmMemberAccts.add(oldAcct.Id);
                    if(AccountTeamMap.get(oldAcct.Id) == null){
                        AccountTeamMap.put(oldAcct.Id, new List<ID>{oldAcct.Industry_Manager__c});}
                    else{
                        AccountTeamMap.get(oldAcct.Id).add(oldAcct.Industry_Manager__c);}
                system.debug('Account teammap 2 : '+ AccountTeamMap);

                
                //add new processor to account team and update sharing rules
                AccountTeamMember im = new AccountTeamMember();
                im.AccountId = a.Id;
                im.TeamMemberRole = 'Industry Manager';
                im.UserId = a.Industry_Manager__c;
                acctMembers.add(im);
                
                AccountShare imSharingRule = new AccountShare();
                imSharingRule.AccountId = a.Id;
                imSharingRule.OpportunityAccessLevel = 'Read';
                imSharingRule.CaseAccessLevel = 'Read';
                imSharingRule.AccountAccessLevel = 'Edit';
                imSharingRule.UserOrGroupId = a.Industry_Manager__c;
                acctSharingRules.add(imSharingRule);
            }
            else if(oldAcct.Industry_Manager__c != a.Industry_Manager__c && a.Industry_Manager__c == null)
            {

                rmMemberAccts.add(a.Id);
                    if(AccountTeamMap.get(oldAcct.Id) == null){
                        AccountTeamMap.put(oldAcct.Id, new List<ID>{oldAcct.Market_Director__c});}
                    else{
                AccountTeamMap.get(oldAcct.Id).add(oldAcct.Client_Advisor__c);}


            }

        }
        
        //DML OPERATIONS
        //remove team members from account team if any exist
        
        acc.add(a);

    }

        system.debug('-->ACCOUNT MEMBERS: ' + acctMembers);
        //insert the new account team members if any exist
        if(acctMembers.size() > 0)
            upsert acctMembers; //LINE 100
        
        //insert account sharing rules if any exist
        if(acctSharingRules.size() > 0)
            upsert acctSharingRules;
            
        if (acc[0].Client_Advisor__c == null )
        {
            list<AccountTeamMember> atm1 = [SELECT Id, UserId, AccountId, TeamMemberRole
                                            FROM AccountTeamMember
                                            WHERE (TeamMemberRole='Client Advisor') AND AccountId =:acc[0].id];
            if (atm1.size() > 0)
            {delete atm1;}
        }
        if (acc[0].Industry_Manager__c == null )
        {
            list<AccountTeamMember> atm1 = [SELECT Id, UserId, AccountId, TeamMemberRole
                                            FROM AccountTeamMember
                                            WHERE (TeamMemberRole='Industry Manager') AND AccountId =:acc[0].id];
            if (atm1.size() > 0)
            {delete atm1;}
        }
        if (acc[0].Market_Director__c == null )
        {
            list<AccountTeamMember> atm1 = [SELECT Id, UserId, AccountId, TeamMemberRole
                                            FROM AccountTeamMember
                                            WHERE (TeamMemberRole='Market Director') AND AccountId =:acc[0].id];
            if (atm1.size() > 0)
            {delete atm1;}
        }    
}

This was selected as the best answer
Team WorksTeam Works

Hi

Trying to do this in Free Org but getting the error

Compile Error: Field is not writeable: AccountShare.AccountId at line 39 column 13 which is 

 caSharingRule.AccountId = a.Id;

Please suggest!

 

Many Thanks in advance

CSECSE
I realize this is an old post but, there is a solution Now Available on the AppExchange! - Custom Account Team Management System (CATMS) (https://appexchange.salesforce.com/appxListingDetail?listingId=a0N3A00000FHBSKUA5) - The CATMS app allows you to build triggers and other automation that will execute when Account Team Member Records are Created, Edited or Deleted, while maintaining and enforcing access security and standard functionality for Account Teams.