I am trying to update Accounts with Account Team information. I have created a custom field on the Account Object and want the Account to update after a team member has been added. I can get the field to populate if I edit and save the account record, but not sure how I can get the Account to update after changes are made to the Account Team.

Also, if I edit and save an account record that does not have a team member it is updating it with the team member on a different account.

trigger UpdateTeamMember on Account (before update, before insert) {
    for (Account acctTeam : Trigger.new) {

    ID teamMem = [select User.Name from AccountTeamMember where TeamMemberRole = 'Manager' limit 1].UserId;
    string userName = [select Name from User where Id = :teamMem].Name;
    acctTeam.Manager__c = userName;


On a final note, if I remove the team member and then attempt to delete the value in the Manager field I get this error:

UpdateTeamMember: execution of BeforeUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.UpdateTeamMember: line 4, column 1

Any help would be appreciated


1 Answer 1


The Error you were getting is caused by trying to get a single record when the 'list' returned by the query is empty.

if the [select User.Name from AccountTeamMember where TeamMemberRole = 'Manager' limit 1] query returns 0 results you will not be able to access the Id of the object returned.

To avoid this you could instead Query the object back into a list, then if the object is not null or empty you can then get the required field from the object in the List.


List<AccountTeamMember> manager = [select Id from AccountTeamMember where TeamMemberRole = 'Manager' limit 1];
If(manager != null && manager.size() > 0)
    //Do something using the manager record here. i.e. managerId = Manager[0].Id; 

The trigger you have written currently is not bulkified and if mass updating accounts you will hit the 101 SOQL queries limit. There is a short explanation of bulkifiying triggers on the Developerforce wiki.

I have attached a modified version of the trigger you have written with changes made commented to try and help out for future reference.

 trigger UpdateTeamMember on Account (before update, before insert) 
    Set<Id> accountIds = new Set<Id>();
    // get the Account Ids updated / inserted to be used in the following query
    for (Account acctTeam : Trigger.new) 
    //SOQL query moved outside of the Loop

    // for each account updated get the related TeamMembers with a 'Manager' Role
    List<Account> accounts = [Select Id, Manager__c, (select User.Name from AccountTeamMembers where TeamMemberRole = 'Manager') From Account Where Id IN :accountIds];

    //Loop over the accounts if any were found
    if(accounts != null && accounts.size() > 0 )
        //Go through the accounts returned from the query
        for(Account acc : accounts)
            //If the manager has been removed, when the
            //account is updated the field will return to null
            if(acc.AccountTeamMembers == null || acc.AccountTeamMembers.size() == 0)
                acc.Manager__c = null;
            else if (acc.AccountTeamMembers.size() == 1)
                acc.Manager__c = acc.AccountTeamMembers[0].User.Name;
                //Many managers were attatched to this account 
                // Choose what to do here 
                // Error?  /  pick first from list? 

I have left the case where an accountTeam has many managers up to your discretion as to what to do here.

  • Michael, thank you for the code. I made the necessary adjustments, but now the field doesn't populate anything.
    – Liz
    Commented Jan 29, 2014 at 17:57
  • have you got 1 AccountTeamMember who is a manager on the account you are testing this with? 0 managers will set the field to null, more than one manager will do no action unless you have added it in. Commented Jan 29, 2014 at 18:01
  • I do have 1 Member on the team that is set to Manager.
    – Liz
    Commented Jan 29, 2014 at 18:53
  • I mocked this up and found that i had the same issue even though the log shows the variable being set on the following line 21:00:25:154 VARIABLE_ASSIGNMENT [28]|this.Manager__c|"Michael Fleming"|0x93f7e97 Commented Jan 29, 2014 at 21:17

You must log in to answer this question.

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