I have custom lookup fields to the user object for various members of our overlay teams, ex. sales engineer.

Can someone help with a trigger to add this user to the account team when the field is filled in at the account level?

  • Welcome to SFSE Stephanie! As it is, this question is a bit too broad hence the 'close question' votes from the community. SFSE questions should typically include a code sample, showing what you've tried and exactly where you've gotten stuck. Good luck! :)
    – Matt Lacey
    Commented Jan 7, 2015 at 23:36

1 Answer 1


Here is the example for update which you can modify for insert and update both.

trigger AccTrig on Account (after update) 
  Map<Id,Id> accShareId = new Map<Id,Id>();
  for(Integer i= 0; i < Trigger.new.size(); i++)
    accShareId.put(Trigger.new[i].Id, Trigger.new[i].yourLookUpFieldName);
    list<AccountTeamMember> lstToInsrt = new list<AccountTeamMember>();
    for(ID i: accShareId.keySet())
     AccountTeamMember  atm = new AccountTeamMember( AccountId = i, UserId = accShareId.get(i));
    if(lstToInsrt.size() > 0)
        insert lstToInsrt;

  • Shailesh -- 1) trigger only handles update on Account event, not insert which is clearly possible given OP; 2) use of i to mean integer index and id value in same code object is not best practice; 3) how does this avoid adding the same ATM on the second update to the same Account (for some unrelated field); 4) How does it remove existing ATM if user changes lookup relationship?
    – cropredy
    Commented Jan 8, 2015 at 2:40
  • @crop1645 : I know the code is not written per best practices. I just gave the basic idea to the user about adding the user to the account team. I'll try to edit my answer though. Thanks for the suggestion! :) Commented Jan 8, 2015 at 6:49
  • @shailesh In re: best practices, see also: error handling, handler pattern (only flow control in trigger body)
    – Adrian Larson
    Commented Jan 8, 2015 at 15:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .