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
chaithaly gowdachaithaly gowda 

required fields in opportunity teammember object


This is the trigger written to add the users to the opportunity team member related list


trigger oppTeammember2 on Opportunity (before insert) {
    
 
    list<opportunityteamMember> opp=new list<opportunityteamMember>();
user u=[select id from user where alias='cgowd' limit 1];
    for(opportunity o:trigger.new)
    {
        if(o.Amount>5000000)
        {
         opportunityteamMember o1=new opportunityteamMember();
           
             o1.TeamMemberRole='Account Manager';
            o1.userId=u.id;
            o1.OpportunityAccessLevel = 'All';
            o1.OpportunityId=o.id;
           
            opp.add(o1);
            
        }
         
        }
   insert opp;
            
    }

I am getting the error as:
oppTeammember2: execution of BeforeInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [OpportunityId]: [OpportunityId] Trigger.oppTeammember2: line 21, column 1
 Any help is appreciable 
Thank you
 
Best Answer chosen by chaithaly gowda
Vikash GoyalVikash Goyal
Hi,

This trigger should be on 'after insert' instead of 'before insert'.  As to insert an opportunity team member record 'OpportunityId' is required which will be available in after insert trigger. i.e. 
trigger oppTeammember2 on Opportunity (after insert) {
    list<opportunityteamMember> opp = new list<opportunityteamMember>();
   user u = [select id from user where alias='cgowd' limit 1];
    for(opportunity o:trigger.new) {
        if(o.Amount>5000000){
            opportunityteamMember o1=new opportunityteamMember();
            o1.TeamMemberRole='Account Manager';
            o1.userId=u.id;
            o1.OpportunityAccessLevel = 'All';
            o1.OpportunityId=o.id;
           
            opp.add(o1);
        }
   }
   insert opp;
}

Please mark this as answer if it works for you.

Thanks​​​​​​​
 

All Answers

Vikash GoyalVikash Goyal
Hi,

This trigger should be on 'after insert' instead of 'before insert'.  As to insert an opportunity team member record 'OpportunityId' is required which will be available in after insert trigger. i.e. 
trigger oppTeammember2 on Opportunity (after insert) {
    list<opportunityteamMember> opp = new list<opportunityteamMember>();
   user u = [select id from user where alias='cgowd' limit 1];
    for(opportunity o:trigger.new) {
        if(o.Amount>5000000){
            opportunityteamMember o1=new opportunityteamMember();
            o1.TeamMemberRole='Account Manager';
            o1.userId=u.id;
            o1.OpportunityAccessLevel = 'All';
            o1.OpportunityId=o.id;
           
            opp.add(o1);
        }
   }
   insert opp;
}

Please mark this as answer if it works for you.

Thanks​​​​​​​
 
This was selected as the best answer
chaithaly gowdachaithaly gowda
Thank you.
 
Tim Wilson 27Tim Wilson 27
Thanks a lot Vikash! I had exactly the same issue in my working project site apkdrod (https://apkdrod.com/)