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
JemillJemill 

I'm having trouble getting my trigger to place a null value in a field when Account Team member is deleted. Old value stays in field on Acocunt

trigger ADAccountTeam on Account (before update, before insert) {
    Set<Id> accountIds = new Set<Id>();
    
    for (Account account: Trigger.new) {
        accountIds.add(Account.Id);
    }
    
    accountIds.remove(null);
    
    Map<Id, Id> accountToTeamMemberMap = new Map<Id, Id>();

    for (AccountTeamMember teamMember: [
        select UserId, AccountId
        from AccountTeamMember
        where Account.Id in :accountIds and
            TeamMemberRole = 'CJ Advertiser Account Director'
    ]) {
        accountToTeamMemberMap.put(teamMember.AccountId, teamMember.UserId);
    }
    
    for (Account account: trigger.new) {
        if (accountToTeamMemberMap.containsKey(Account.Id)) {
            Account.CJ_Advertiser_Account_Director__c = accountToTeamMemberMap.get(Account.Id);
        }
    }

}



So this trigger puts the Account Team member into an Account field when the role = CJ Advertiser Account Director.  The 2 issues I'm having trouble with is when I delete the Account Team member off, the field on the Account doesn't delete the user from the field when Account is edited.  My second question is:  Is there a way for this Trigger to execute without someone editing the actual Account?  So when the Account Team member is added it would automatically update the Account field.
Best Answer chosen by Jemill
Deepak GulianDeepak Gulian
trigger ADAccountTeam on Account (before update, before insert) {
    Set<Id> accountIds = new Set<Id>();
    
    for (Account account: Trigger.new) {
        accountIds.add(Account.Id);
    }
    
    accountIds.remove(null);
    
    Map<Id, Id> accountToTeamMemberMap = new Map<Id, Id>();

    for (AccountTeamMember teamMember: [
        select UserId, AccountId
        from AccountTeamMember
        where Account.Id in :accountIds and
            TeamMemberRole = 'CJ Advertiser Account Director'
    ]) {
        accountToTeamMemberMap.put(teamMember.AccountId, teamMember.UserId);
    }
    
    for (Account account: trigger.new) {
        if (accountToTeamMemberMap.containsKey(Account.Id)) {
            Account.CJ_Advertiser_Account_Director__c = accountToTeamMemberMap.get(Account.Id);
        }
       else{
          Account.CJ_Advertiser_Account_Director__c =  null;
       }
    }
Try this!
 

All Answers

R Z KhanR Z Khan
The trigger doesnt work on before insert because none of the accounts have an Id, so your accountIds is always empty on isnert. try doing after insert. I don't see how team member is getting deleted from your code though.
JemillJemill
I moved to after insert and it still requires me to edit the Account to get the user to be passed through to the Field.  For "I don't see how team member is getting deleted from your code though." are you saying I need to edit my code so that when there is no team member of that role it deletes any user from the Account field?  Sorry, a bit novice to coding.
R Z KhanR Z Khan
Your question says "when Account Team member is deleted". Are you sure there are account team member who have that name adn correspond to the account id you have?
Deepak GulianDeepak Gulian
trigger ADAccountTeam on Account (before update, before insert) {
    Set<Id> accountIds = new Set<Id>();
    
    for (Account account: Trigger.new) {
        accountIds.add(Account.Id);
    }
    
    accountIds.remove(null);
    
    Map<Id, Id> accountToTeamMemberMap = new Map<Id, Id>();

    for (AccountTeamMember teamMember: [
        select UserId, AccountId
        from AccountTeamMember
        where Account.Id in :accountIds and
            TeamMemberRole = 'CJ Advertiser Account Director'
    ]) {
        accountToTeamMemberMap.put(teamMember.AccountId, teamMember.UserId);
    }
    
    for (Account account: trigger.new) {
        if (accountToTeamMemberMap.containsKey(Account.Id)) {
            Account.CJ_Advertiser_Account_Director__c = accountToTeamMemberMap.get(Account.Id);
        }
       else{
          Account.CJ_Advertiser_Account_Director__c =  null;
       }
    }
Try this!
 
This was selected as the best answer
R Z KhanR Z Khan
trigger ADAccountTeam on Account (before update, before insert) {
    Set<Id> accountIds = new Set<Id>();
    if(trigger.isupdate){
    for (Account account: Trigger.new) {
        accountIds.add(Account.Id);
    }

    
    Map<Id, Id> accountToTeamMemberMap = new Map<Id, Id>();

    for (AccountTeamMember teamMember: [
        select UserId, AccountId
        from AccountTeamMember
        where Account.Id in :accountIds and
            TeamMemberRole = 'CJ Advertiser Account Director'
    ]) {
        accountToTeamMemberMap.put(teamMember.AccountId, teamMember.UserId);
    }
    
    for (Account account: trigger.new) {
            Account.CJ_Advertiser_Account_Director__c = accountToTeamMemberMap.get(Account.Id);
    }
}
if(trigger.isInsert){
  for (Account account: trigger.new) {
            Account.CJ_Advertiser_Account_Director__c = null;
    }
}
Use the code above. During the isnert of the account there wont be any account team members associated with this account. so you dont need to query anything jsut loop through the accoutns and set your field to null durign insert trigger. 
 
JemillJemill
Thanks Deepak and R Z Khan, both solved the issue of deletion of Account Team member not deleting the user from the field!  I'll ask around some more on how to have these update the Account fields without having to edit the Account manually (executes right when I save/delete an Account Team member) and if it's possible
R Z KhanR Z Khan
You cant crate a trigger on AccountTeamMember. You can vote here for the idea
https://success.salesforce.com/ideaView?id=08730000000YR8IAAW