When we create the Opportunity with the Probability=20, then the opportunity owner will be automatically added to Account Team of the associated account for that Opportunity.
trigger UpdateATMwithOwneronOptyCreate on Opportunity(after insert, after update) {
List < AccountShare > list_share = new List < AccountShare > ();
List < AccountTeamMember > list_atm = new List < AccountTeamMember > ();
for (Opportunity opp: Trigger.New)
{
if (opp.Probability == 20)
{
AccountTeamMember atm = new AccountTeamMember();
atm.accountid = opp.accountid;
atm.teamMemberRole = 'Account Manager';
atm.UserId = opp.Ownerid;
AccountShare share = new AccountShare();
share.AccountId = opp.Accountid;
share.UserOrGroupId = opp.OwnerId;
share.AccountAccessLevel = 'Read/Write';
share.OpportunityAccessLevel = 'Read Only';
share.CaseAccessLevel = 'Read Only';
list_atm.add(atm);
list_share.add(share);
}
}
if (list_atm != null)
insert list_atm;
if (list_share != null && list_share.size() > 0)
List < Database.saveresult > sr = Database.insert(list_share, false);