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 

Update Account Team Record from Account

Hello,

 

I have created 3 custom user look-up fields at the Account level to represent the 3 roles in the Account Team.  I have a trigger that is supposed to update the Account Team records when the Account is updated (see below).  The trigger saves fine, but when I edit the Account with values in the 3 custom fields, I get the following error:

 

Error:Apex trigger AccountTeamChanges caused an unexpected exception, contact your administrator: AccountTeamChanges: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_ACCESS_LEVEL, : AccountAccessLevel, OpportunityAccessLevel, CaseAccessLevel (Account, Opportunity, Case Levels, Con Levels (Read, Read, Read, Read) are below organization levels (Read, Read, ReadEditTransfer, ControlledByParent)): [AccountAccessLevel, OpportunityAccessLevel, CaseAccessLevel]: Trigger.AccountTeamChanges: line 199, column 1

 

My Triger code is as follows:

 

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, ID> AccountTeamMap = new Map<ID, ID>();
    
     //iterate through records to find update Account Team 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 = 'Read';
            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 = 'Read';
            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
            if(oldAcct.Client_Advisor__c != a.Client_Advisor__c && a.Client_Advisor__c != null)
            {
                //add old Client Advisor to remove list if one exists
                if(oldAcct.Client_Advisor__c != null)
                {
                    rmMemberAccts.add(oldAcct.Id);
                    AccountTeamMap.put(oldAcct.Id, oldAcct.Client_Advisor__c);
                }
                
                //add new Client Advisor 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 = 'Read';
                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);
                AccountTeamMap.put(oldAcct.Id, 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 Market Director to remove list if one exists
                if(oldAcct.Market_Director__c != null)
                {
                    rmMemberAccts.add(oldAcct.Id);
                    AccountTeamMap.put(oldAcct.Id, oldAcct.Market_Director__c);
                }
                
                //add new Market Director 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 = 'Read';
                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);
                AccountTeamMap.put(oldAcct.Id, oldAcct.Market_Director__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 Industry Manager to remove list if one exists
                if(oldAcct.Industry_Manager__c != null)
                {
                    rmMemberAccts.add(oldAcct.Id);
                    AccountTeamMap.put(oldAcct.Id, oldAcct.Industry_Manager__c);
                }
                
                //add new Industry Manager to account team and update sharing rules
                AccountTeamMember im = new AccountTeamMember();
                im.AccountId = a.Id;
                im.TeamMemberRole = 'Industry Manager';
                im.UserId = a.Market_Director__c;
                acctMembers.add(im);
                
                AccountShare imSharingRule = new AccountShare();
                imSharingRule.AccountId = a.Id;
                imSharingRule.OpportunityAccessLevel = 'Read';
                imSharingRule.CaseAccessLevel = 'Read';
                imSharingRule.AccountAccessLevel = 'Read';
                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);
                AccountTeamMap.put(oldAcct.Id, oldAcct.Industry_Manager__c);
            }

        }
        
        //DML OPERATIONS
        //remove team members from account team if any exist
        if(rmMemberAccts.size() > 0)
        {
            List<AccountTeamMember> removeTeam = new List<AccountTeamMember>();
            for(AccountTeamMember atm : [SELECT Id, UserId, AccountId
            FROM AccountTeamMember
            WHERE (TeamMemberRole='Client Advisor' OR TeamMemberRole='Market Director' OR TeamMemberRole='Industry Manager') AND AccountId IN :rmMemberAccts])
            {
                if(atm.UserId == AccountTeamMap.get(atm.AccountId))
                    removeTeam.add(atm);
            }
            
            delete removeTeam;
        }
        
        system.debug('-->ACCOUNT MEMBERS: ' + acctMembers);
        //insert the new account team members if any exist
        if(acctMembers.size() > 0)
            insert acctMembers; //LINE 100
        
        //insert account sharing rules if any exist
        if(acctSharingRules.size() > 0)
            insert acctSharingRules;
     }
     
}

 

 

Can anyone help me understand why I am getting this error?  Thanks,

Best Answer chosen by Admin (Salesforce Developers) 
JN22JN22

Hi,

 

Yes the issue was resolved.   The problem was that I was trying to add the userid to the Map but it was only able to contain 1 id when multiple were needed.  To avoid it I had to create a Map of IDs.  Below is my final trigger.

 

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;

}

All Answers

jungleeejungleee

Hi,

 

Can you change the OWD setting of Account in sharing settings page to PRIVATE and check if the issue stil shows up?? When you change the access level of account to private, the access level for case, contact and opp will also change to Private.

 

-Sam

 

 

darcusmietzdarcusmietz

Hey there, I'm have the same issue-- also with custom fields. Did you ever find a resolution?

JN22JN22

Hi,

 

Yes the issue was resolved.   The problem was that I was trying to add the userid to the Map but it was only able to contain 1 id when multiple were needed.  To avoid it I had to create a Map of IDs.  Below is my final trigger.

 

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;

}

This was selected as the best answer