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
Erin Rico-Allen 2Erin Rico-Allen 2 

Creating a before trigger based on Account Teams - Need help with code

So we have these two look-up(user) fields on the Account object:
  • Account_Manager__c
  • Enterprise_Solutions_Manager__c
On Account Teams, we have 2 team role picklist values that correspond to these fields (api names):
  • Account Manager
  • Enterprise Solution Manager
I need a before trigger that will update the account fields automatically based on the Account Team roles setup based on the owner of the account (so each owner has a pre-set account team).

I am not a developer and I don't really write code. Can someone help me out with this?

Thanks!
Erin
Leon ThanikalLeon Thanikal
Hello Erin,

Just a forewarning I have only begun developing in Salesforce recently. This is my attempt at solving your question. If it helps great, if not, hopefully it becomes the basis for the solution.
Also a note, it generally is not a good practice to use a before trigger to update another object. You can, but with an after trigger you will be able to capture the OBJ.Id so that you can use it later on.
I also use a trigger handler class so that it keeps the trigger clean.

The Trigger:
trigger AccountTeamMemberTrigger on AccpountTeamMember (before insert, after insert, before update, after update) {
    if(Trigger.isBefore){
        if(Trigger.isInsert){
            //todo Don't need it now, but good practice to have a single trigger
            //todo for every object
        }
        if(Trigger.isUpdate){
            //todo Don't need it now, but good practice to have a single trigger
            //todo for every object
        }
    }
    //! You actually need to run the update for another object in an After trigger
    //! This way you can get the OBJ.Id from it, just in case you need it
    if(Trigger.isAfter){
        if(Trigger.isInsert){
            AccountTeamMemberTriggerHandler.addManagers(Trigger.New, null, false);
        }
        if(Trigger.isUpdate){
            AccountTeamMemberTriggerHandler.addManagers(Trigger.New, null, false);
        }
    }
}

The Trigger Handler:
public with sharing class AccountTeamMemberTriggerHandler {
    
    public static void addManagers(List<AccountTeamMember> lstNew, List<AccountTeamMember> lstOld, Boolean isDel){
        
        List<AccountTeamMember> acctTeam = new List<AccountTeamMember>();
        if(isDel == false){
            if(lstNew != null){
                acctTeam = lstNew;
            }            
        } else {
            //todo not really needed in this trigger but w/e
            System.debug('You are here')!
        }
        List<Account> acct = new List<Account>();
        for(AccountTeamMember a : acctTeam){
            Account at = new Account();
            at.id = a.AccountID;
            at.Account_Manager__c = a.Account_Manager__c;
            at.Enterprise_Solutions_Manager__c = a.Enterprise_Solutions_Manager__c;

            acct.add(at);
        }

        if(acct.size() > 0){
            upsert acct;
        }
    }
}

Not really sure if I used the correct API names so please do change them.

Hope this helps!