+ Start a Discussion
Bill NicholsonBill Nicholson 

Can someone help me to write a trigger that updates the account team member (adds) when a owner is edited and the record type is household.

sukanya banekarsukanya banekar
Following is the code to set account team member if owner of the account has changed
trigger Accountteam on Account (after update, after Insert) {
        Id recordType= Schema.SObjectType.Account.getRecordTypeInfosByName().get('Household').getRecordTypeId();
        List<AccountTeamMember> lstTeam = new list<AccountTeamMember>();
        for(Account objAcc: trigger.newMap.values()){
            if(objAcc.OwnerId <> trigger.oldMap.get(objAcc.Id).OwnerId
              && objAcc.RecordTypeId == recordType ){
                AccountTeamMember objAccTeam = new AccountTeamMember();
                objAccTeam.AccountId = objAcc.id;
                objAccTeam.UserId = objAcc.OwnerId;
                objAccTeam.TeamMemberRole= 'Account Manager'; // you can change the role
        if(lstTeam<> null && !lstTeam.isEmpty())
            insert lstTeam;

Also replace TeamMemberRole with the value you want.

Hope this helps you.

Sukanya Banekar