• CSE
  • NEWBIE
  • 0 Points
  • Member since 2019
  • The Cloud Solution Experts,LLC

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
All, below is my trigger which increases sharing access when a new Account Team Member is entered on an account. Trigger works fine but this trigger does not run when new Account Team Member is created. It only works when account is updated (which I understand because I have created a trigger on Account object). From what I understand, triggers cannot be created on Account Team Member. Is there a workaround?
 
trigger teamsharing on Account (before update) {
    
          List<AccountShare> shares = new List<AccountShare>();
        Set <ID> accids = New Set <ID> ();
         For ( Account A: Trigger.New) {
            accids.add(A.id);
    }

       List <AccountTeamMember> ATMs = [SELECT AccountId, TeamMemberRole, UserId, AccountAccessLevel FROM AccountTeamMember WHERE AccountId in: accids];
    
    For (AccountTeamMember AT : ATMs ) {

    
                     AccountShare aShare = new AccountShare();
          aShare.AccountId              = AT.AccountId;
          aShare.UserOrGroupId          = AT.UserId;
          aShare.AccountAccessLevel     = 'Edit';
          aShare.ContactAccessLevel     = 'Edit';
          aShare.OpportunityAccessLevel = 'Edit';
          shares.add(aShare);
    
        
    }
    Insert Shares;
}

 

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;

}

  • July 10, 2013
  • Like
  • 0