Trigger to add Opportunity owner in Opportunity team

Sample Trigger:

trigger OpportunityTrigger on Opportunity (after insert) {
    List<OpportunityTeamMember> listOpptyTeamMem = new List<OpportunityTeamMember>();
    
    for(Opportunity oppty : trigger.New){
        OpportunityTeamMember OTM = new OpportunityTeamMember();
        OTM.OpportunityId = oppty.Id;
        OTM.TeamMemberRole = 'Sales Rep'; 
        OTM.UserId = oppty.OwnerId;
        listOpptyTeamMem.add(OTM);             
    }
    
    if(listOpptyTeamMem.size() > 0){
        insert listOpptyTeamMem;
    }
}

No comments:

Post a Comment