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
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
Hi,
Following is the code to set account team member if owner of the account has changed
 
trigger Accountteam on Account (after update, after Insert) {
    if(trigger.isAfter){
        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
                lstTeam.add(objAccTeam);
            }            
        }
        if(lstTeam<> null && !lstTeam.isEmpty())
            insert lstTeam;
    }
}


Also replace TeamMemberRole with the value you want.

Hope this helps you.

Thanks,
Sukanya Banekar