+ Start a Discussion
Vijay Zutshi 2Vijay Zutshi 2 

opportunityAcountTeam caused an unexpected exception


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) {  
                    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.OpportunityAccessLevel = 'Read Only';
                     //newAccShare.CaseAccessLevel='Read Only';
                    //newAccShare.AccountId = newOpp.AccountId;
                    //newAccShare.UserOrGroupId = newOpp.OwnerId;                    
                        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.

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){
            for(Opportunity opp: allOpps){
                AccountTeamMember newAccMember = new AccountTeamMember();
                newAccMember.AccountId = opp.AccountId;
                //add all fields here
            insert TeamMembersToInsert;


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