Hi, In this scenario we have shown, Whenever new Account record is created with Industry as “Education” and annual Revenue as “5 L” then add user Test as AccountTeamMember.
AccountTeamMember :
1.AccountId : Id of the record on which we are creating teammember
2.UserId : Id of the user whom we are adding as Teammember
3.TeamMemberRole : choose some value pickList field of TeamMemberRole
4.AccountAccessLevel : Type of Access you are providing on Account for the team member
Possible values are ‘Read’ ,’Edit’,’ALL’
Note : Before we implement this trigger enable the AccountTeams
setup
|----Build
|----Customize
|------Accounts
|------AccountTeams
1. Enable AccountTeam
2. Add the AccountTeams as related list on all the pageLayouts
Trigger
trigger AccountTeamTrig on Account (after insert) {
List<AccountTeamMember> teams=new List<AccountTeamMember>();
User u=[select id from User where alias='test'];
for(Account a:Trigger.New){
if(a.Industry=='Education' && a.AnnualRevenue > 500000){
AccountTeamMember atm=new AccountTeamMember();
atm.accountId=a.id;
atm.userId=u.id;
atm.TeamMemberRole='Account Manager';
atm.AccountAccessLevel='Edit';
teams.add(atm);
}
}
insert teams;
}
Test Class
@isTest
private class AccountTeamTest {
@isTest
static void testme(){
User u=[select id from User where alias='test'];
Account a=new Account();
a.Industry='Education';
a.name='test';
a.AnnualRevenue=800000;
try{
insert a;
}catch(Exception e){
System.debug(e);
}
AccountTeamMember atm=[select userId,AccountId,AccountAccessLevel from AccountTeamMember where AccountId=:a.id];
if(a.Industry=='Education' && a.AnnualRevenue>500000){
System.assertEquals(atm.userId,u.id);
System.assertEquals(atm.AccountAccessLevel,'Edit');
System.assertEquals(atm.TeamMemberRole,'Account Manager');
}else{
System.assertEquals(atm,null);
}
}
}