0

I have successfully created a trigger that will add a Team Member when a custom field on the Account object is populated, but I am now trying to add code that will remove that same Team Member when the custom field is blank. This is what I have, but the Team Member is not being deleted. Thanks

Trigger:

trigger UpdateBankRepTeam on Account (After update, After insert){
    UpdateBRHelper.updateTeamMember(trigger.new);
}

Class:

   public class UpdateBRHelper{
        public static void updateTeamMember(List<Account> accounts){

            List<AccountShare> ashareLIST = new List<AccountShare>();
            List<AccountTeamMember> ATM = new List<AccountTeamMember>();
            Set<String> rmMember = new Set<String>();
            Map<ID, ID> acctToBRepMap = new Map<ID, ID>();

            for(Account a:accounts){

                AccountShare caSharingRule = new AccountShare();
                   caSharingRule.AccountId = a.Id;
                   caSharingRule.OpportunityAccessLevel = 'Edit';
                   caSharingRule.CaseAccessLevel = 'Edit';
                   caSharingRule.AccountAccessLevel = 'Edit';
                   caSharingRule.UserOrGroupId = a.Banking_Rep__c;
                   ashareLIST.add(caSharingRule);

               if(a.Banking_Rep__c != null){
                  AccountTeamMember Teammemberad=new AccountTeamMember();
                  Teammemberad.AccountId=a.id;
                  Teammemberad.UserId=a.Banking_Rep__c;
                  Teammemberad.TeamMemberRole = 'Banking Rep';
                  ATM.add(Teammemberad);
            }
            if(!ATM.isEmpty()){
               insert ATM;           
               insert ashareLIST;
               }  
            if(rmMember.size() > 0)
            {
            List<AccountTeamMember> removeTeam = new List<AccountTeamMember>();
                for(AccountTeamMember Ratm : [SELECT Id, UserId, AccountId FROM AccountTeamMember WHERE TeamMemberRole='Banking Rep' AND AccountId IN :rmMember])
                {
                    if(Ratm.UserId == acctToBRepMap.get(Ratm.AccountId))
                        removeTeam.add(Ratm);
                }

                delete removeTeam;
            }     
            }
        }
     }
  • There is no where in your code that you are checking for the Banking_Rep__c to be null. I am confused on your logic as in you code you are: 1. If the Banking rep is NOT null, remove them if they are on the account team. Thus if the field is null and was previously set t a value that Team Member should have been previously removed. Thus if it is null there is nothing to remove.... – Eric May 18 '14 at 23:59
1

I dont see how your SET and MAP are filled in the above code

 Set<String> rmMember = new Set<String>();
 Map<ID, ID> acctToBRepMap = new Map<ID, ID>();

The above variables are not populated .

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.