+ Start a Discussion
Bill NicholsonBill Nicholson 

Can someone help me write a trigger on the Account Object -Use Case: When account owner is edited, and t he record type is household, add edited owner to account team member.

Glyn Anderson 3Glyn Anderson 3
Bill,  I think the code below does what you describe.  If the Account owner changes (including when the Account is first created), then we check to see if they are already an AccountTeamMember for that Account.  If not, we create a new AccountTeamMember record for them.  Let me know if you have any problems or questions.  Disclaimer:  This code is untested and may contain typos.

<pre>
trigger AccountOwnerIsTeamMember on Account ( after insert, after update )
{
    // query the household record type so we don't hard-code the ID
    RecordType householdRT =
        [   SELECT  Id
            FROM    RecordType
            WHERE   SObjectType = 'Account' AND DeveloperName = 'Household'
        ];

    // find household accounts whose owner has changed
    Set<Id> householdsWithNewOwners = new Set<Id>();
    for ( Account account : Trigger.new )
    {
        if ( account.RecordTypeId != householdRT.Id ) continue;

        Account oldAccount = Trigger.isUpdate ? Trigger.oldMap.get( account.Id ) : null;
        if ( oldAccount == null || oldAccount.OwnerId != account.OwnerId )
        {
            householdsWithNewOwners.add( account.Id );
        }
    }

    Id currentUserId = UserInfo.getUserId();

    // get current user's existing account team members for those accounts
    Map<Id,Account> householdsWithAccountTeamMembers = new Map<Id,Account>
    (   [   SELECT  Id
                (   SELECT  Id
                    FROM    AccountTeamMembers
                    WHERE   UserId = :currentUserId
                )
            FROM    Account
            WHERE   Id IN :householdsWithNewOwners
        ]
    );

    // create new account team members where necessary
    List<AccountTeamMember> accountTeamMembersToInsert = new List<AccountTeamMember>();
    for ( Id accountId : householdsWithNewOwners )
    {
        // if the current user is not already a team member, make them be one
        Account household = householdsWithAccountTeamMembers.get( accountId );
        if ( household == null || household.AccountTeamMembers.isEmpty()
        {
            accountTeamMembersToInsert.add
            (   new AccountTeamMember
                (   AccountAccessLevel = 'Edit'
                ,   AccountId = accountId
                ,   CaseAccessLevel = 'Edit'
                ,   ContactAccessLevel = 'Edit'
                ,   OpportunityAccessLevel = 'Edit'
                ,   TeamMemberRole = 'Account Manager'
                ,   UserId = currentUserId
                )
            );
        }
    }

    // insert new account team members, if any
    insert accountTeamMembersToInsert;
}
</pre>
 
Glyn Anderson 3Glyn Anderson 3
Bill, I found a typo.  Add a comma to the end of line 27, after "SELECT Id".
Glyn Anderson 3Glyn Anderson 3
Bill,  Did this help you solve your problem?  If not let me know.  I'm happy to continue helping.  If it did, please mark the question as solved.  If you solved the problem another way, please post your solution and mark it as the best solution.  Thanks!