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
Melissa FitzgeraldMelissa Fitzgerald 

Apex Trigger for Account Team

We currently have a trigger that was set up by a third party vendor automatically adds an Account Team based on the linked account data of our syncing database.  There is one representaive that the trigger doesn't seem to be recognizing.

I'm unfamiliar with the coding to be able to pin point the issue.  Is there anyone that can point me in the direction of a solution?
Abhishek BansalAbhishek Bansal
Hi Mellisa,

There might be some filters used in the query and the representaive that the trigger isn't recognizing, may be not matching the criteria for the query. It would be good if you can paste the trigger code here so that we can have a look and suggest something to you.
If there is anything urgent you can recat out ot me at gmail or skype:
Gmail: abhibansal2790@gmail.com
Skype: abhishek.bansal2790

Thanks,
Abhishek Bansal.
Melissa FitzgeraldMelissa Fitzgerald
Abhishek,

Here is the trigger code.  Thank you for your help.

trigger AddAccountTeamMembers on Account (after insert,after update){
 //Get User's Name,Id and Store into userMap
   Map<String,Id> userMap=new Map<String,Id>();
   List<Id> userIds= New List<Id>();
   for(User u:[Select id,name from user]){
       userMap.put(u.name,u.id);
       userIds.add(u.Id);
    }
  if(trigger.isInsert && trigger.isAfter){
    Set<Id> repId=new Set<Id>();
    Set<Id> accList=new Set<Id>();
    List<AccountTeamMember> memberList=new List<AccountTeamMember>();   
  //Add account repid and account id's
    for(Account acc : trigger.new){
      repId.add(acc.OASP__OAS_Rep__c);
      accList.add(acc.id);
    }
    Map<id,String> conMap=new Map<id,String>();
  //Query the contacts with account RepId's
    if(repId != null && !repId.isEmpty()){
       for(Contact con: [Select id,name from Contact where Id In:repId]){
          conMap.put(con.id,con.name);
       }
    }
    for(Account acc : trigger.new){
  //Splitting the contacts
     String contactName = conMap.get(acc.OASP__OAS_Rep__c);
     if(contactName!=null){
        List<String> conSplit=contactName.Split(' and ');
  //Add the contacts into Account Team Members
        for(String temp: conSplit){
             AccountTeamMember accMember=new AccountTeamMember();
             accMember.TeamMemberRole='Account Manager';
             accMember.AccountId=acc.id;
             accMember.UserId=(Id)userMap.get(temp);
             memberList.add(accMember);
             
        }
      } 
    } 
    System.debug('++Acc Team Member++'+memberList);
   //insert the contacts and Sharing rules
    if(memberList!=null){
      Database.insert(memberList,false);
    }
    System.debug('++Account List++'+accList);
    //Use future method for Insert Account Share Access
    if(accList != null && accList.size()>0 && memberList.size()>0)
    FutureAccountShare.updateAccountShareAccess(accList);
  }
  if(trigger.isUpdate && trigger.isAfter){
    Set<Id> accId=new Set<Id>();
    Set<Id> repId=new Set<Id>();
    Map<String,AccountTeamMember> accTeamMap = New Map<String,AccountTeamMember>();
    list<AccountTeamMember> accmemlist = New list<AccountTeamMember>();
    list<AccountTeamMember> accteamMembers = New list<AccountTeamMember>();
    Map<id,String> conMap=new Map<id,String>();   
    Set<AccountTeamMember> uniquedelete = new Set<AccountTeamMember>();
    
     for(Account acct: trigger.new){
        accId.add(acct.Id);
        repId.add(acct.OASP__OAS_Rep__c);
     }
     for(Account acct: trigger.old){
        repId.add(acct.OASP__OAS_Rep__c);
     }
     
     For(AccountTeamMember acctmem: [Select id,AccountId,UserId,AccountAccesslevel,TeamMemberRole  from AccountTeamMember where AccountId In: accId]){
          accTeamMap.put(acctmem.AccountId+'&'+acctmem.UserId,acctmem);
          accmemlist.Add(acctmem);
     }
     if(repId != null && !repId.isEmpty()){
       for(Contact con: [Select id,Name from Contact Where ID IN: repId]){
         conMap.put(con.id,con.name);
       }
     }
     Set<Id> accupdateId=new Set<Id>();
     for(Account temp: trigger.New){
      if(temp.OASP__OAS_Rep__c != Trigger.oldMap.get(temp.id).OASP__OAS_Rep__c){
       accupdateId.add(temp.Id);
       for(Id userTemp: userIds){
        String Name = conMap.get(Trigger.oldMap.get(temp.id).OASP__OAS_Rep__c);
        if(Name!=null){
        List<String> contSplitdelete =Name.Split(' and ');
         for(String str: contSplitdelete){
            AccountTeamMember AccmemId = accTeamMap.get(temp.Id+'&'+userMap.get(str)); // 
            if(AccmemId != null) {
              uniquedelete.add(AccmemId);
            }
          }
        }
       }
      } 
     }
     if(uniquedelete!=null){ 
         List<AccountTeamMember> finalDelete = new list<AccountTeamMember>();
         finalDelete.addALL(uniquedelete);
         Database.delete(finalDelete,false);
     }
     List<AccountTeamMember> teamMember=new List<AccountTeamMember>();
        for(Account accUpdate : trigger.new){
        if(accUpdate.OASP__OAS_Rep__c != Trigger.oldMap.get(accUpdate.id).OASP__OAS_Rep__c){
         String contName = conMap.get(accUpdate.OASP__OAS_Rep__c);
          if(contName!=null){
             List<String> contSplit=contName.Split(' and ');
             for(String temp1: contSplit){
                AccountTeamMember acctMember=new AccountTeamMember();
                acctMember.TeamMemberRole='Account Manager';
                acctMember.AccountId=accUpdate.id;
                acctMember.UserId=(Id)userMap.get(temp1);
                teamMember.add(acctMember);          
             }
          }
        } 
        }
     if(teamMember!=null){
     //Insert Account Team Members and Sharing rules for update
      Database.insert(teamMember,false);
     }
 //Use future method for Update Account Share Access
 if(accupdateId != null && accupdateId.size()>0){
    FutureAccountShare.updateAccountShareAccess(accupdateId);
  }
  }
}