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
Vijay Zutshi 2Vijay Zutshi 2 

opportunityAcountTeam caused an unexpected exception

Hi, 

When I 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. My tigger is as follows:- 

trigger opportunityAcountTeam on Opportunity (after insert, after update) {
    LIST<Opportunity> listOpp = new LIST<Opportunity>();
    SET<ID> oppIds = new SET<ID>();
    LIST<AccountTeamMember> listAccTeamMember = new LIST<AccountTeamMember>();
    //LIST<AccountShare> listShare = new LIST<AccountShare>();
    if(Trigger.IsAfter && Trigger.IsInsert) {
        for(Opportunity newOpp : Trigger.new) {
            if(newOpp.Probability == 20) {  
                listOpp.add(newOpp);
                oppIds.add(newOpp.AccountId);
                    AccountTeamMember newAccMember = new AccountTeamMember();
                    newAccMember.AccountAccessLevel = 'Edit';
                    newAccMember.OpportunityAccessLevel = 'Read';
                    newAccMember.TeamMemberRole = 'Account Manager';
                    newAccMember.AccountId = newOpp.AccountId;
                    newAccMember.UserId = newOpp.OwnerId;
                    //AccountShare newAccShare = new AccountShare();
                    //newAccShare.AccountAccessLevel='Read';
                     //newAccShare.OpportunityAccessLevel = 'Read Only';
                     //newAccShare.CaseAccessLevel='Read Only';
                    //newAccShare.AccountId = newOpp.AccountId;
                    //newAccShare.UserOrGroupId = newOpp.OwnerId;                    
                    listAccTeamMember.add(newAccMember);
                    //listShare.add(newAccShare);
        
                        if(listAccTeamMember != NULL) {
                        insert listAccTeammember;                        
                        }
           }
        }       
    }
}

I am getting following error:-

opportunityAcountTeam: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Account]: [Account]: Trigger.opportunityAcountTeam: line 27, column 1

Kindly advise as I am not able to solve the problem.

Thanks
Vijay Zutshi
Sainath VenkatSainath Venkat
hello @Vijay

please try below code.

Apex class:
public class opportunityAcountTeam {
    public static void opportunityAcountTeam(List<Opportunity> allOpps){
        List<AccountTeamMember> TeamMembersToInsert = new List<AccountTeamMember>();
        set<Id> accountId = new set<Id>();
        for(Opportunity opp: allOpps){
            if(opp.AccountId != null && opp.Probability == 20){
                accountId.add(opp.AccountId);
            }
        }
        if(!accountId.isEmpty()){
            for(Opportunity opp: allOpps){
                AccountTeamMember newAccMember = new AccountTeamMember();
                newAccMember.AccountId = opp.AccountId;
                //add all fields here
                TeamMembersToInsert.add(newAccMember);
            }
            insert TeamMembersToInsert;
        }
    }

}

Trigger:
trigger opportunityAcountTeam on Opportunity(after Insert, after Update){
      opportunityAcountTeam.opportunityAcountTeam (trigger.new);
}