Trigger On Opportunity Team Member

Last Updated on November 12, 2017 by Rakesh Gupta

Opp Team Member Task assignment

An opportunity team is a set of users that normally work together on sales opportunities. A typical opportunity team might include the account manager, the sales representative, and a pre-sales consultant. As soon as you assign members in your Opportunity team and if you want to assign some task to them, So you can’t achieve this by using Workflow because there is no such option for task assignment / New Task for ‘Opportunity Team Member‘.

No Option to assign task
No Option to assign task

To achieve this you have to write Apex trigger. Below is the sample code you can modify it according to your requirement.

trigger TaskOnOppTeamMember on OpportunityTeamMember (after insert) {
list<Task> NewTask = new list<Task>();
if(trigger.IsInsert)
{
for(OpportunityTeamMember oppTeam : trigger.new)
{
Task tasksInsert = new Task();
tasksInsert.WhatId = OppTeam.Opportunityid;
tasksInsert.OwnerId = oppTeam.Userid;
tasksInsert.Subject = 'Great Daddy';
tasksInsert.ActivityDate = date.today();
tasksInsert.Priority = 'Normal';
NewTask.add(tasksInsert);

}
}
Database.insert(NewTask);
}

Now navigate to your opportunity detail page Under Opportunity Team related list Click on “Add” Button.

Opp Team Add Button

Select the User Name, Opportunity Access, and Role and click on”Save” button. Now navigate to Open Activity related list and cheers.

Task Assign

3 thoughts on “Trigger On Opportunity Team Member

  1. Hi Rakesh I have a requirement where Opportunity is a custom object and we want to enable Opportunity team concept on that either as related list or by button how can we do this?

    1. In such case, you have to create a custom object to store Opportunity team members. Then use Process Builder and Visual Workflow to share the Opportunity access to opportunity team member as soon as a new record gets created in Team Member object.

  2. Hi Rakesh, thank you for such nice example. I created custom object OpportunityTeamHistory to track changes on Opportunity member. Everything works fine except user click on Add default team button. Do you know why? Please see bellow my trigger:

    trigger addOpportunityTeamHistory on OpportunityTeamMember (after insert, before delete, after update) {
    list otHistoryList = new list();
    Opportunity_Team_History__c otHistory;
    String errorAction;
    List TriggerList = new List ();
    Id CurrentUser = UserInfo.getUserId();

    if (trigger.isAfter && trigger.IsInsert) {
        TriggerList = trigger.new;
        errorAction = (' added to team with ');
    }
    else if (trigger.isBefore && trigger.isDelete) {
        TriggerList = trigger.old;
        errorAction = (' removed from team with ');
    }
    for(OpportunityTeamMember oppTeam : TriggerList){ 
        otHistory = new Opportunity_Team_History__c();
        otHistory.Date__c = datetime.now();
    otHistory.Opportunity__c = oppTeam.OpportunityId;
        otHistory.User__c = CurrentUser;
        otHistory.Action__c = (oppTeam.UserName__c+errorAction+oppTeam.OpportunityAccessLevel+' visibility');
        otHistoryList.add(otHistory); 
    }
    Database.insert(otHistoryList);
    

    }

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.