Trigger On Opportunity Team Member

Trigger On Opportunity Team Member

Last Updated on October 17, 2021 by Rakesh Gupta

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 achieve this by using Salesforce Flow, but we will write Apex Trigger as our goal in to learn Apex programming.

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 = 'Big 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 Opportunity Team members Button.

Select the User, Team Role and Opportunity Access and click on the Save button.

Now navigate to Activity Timeline and validate the task, cheers!

Proof of Concept

Formative Assessment:

I want to hear from you!

What did you learn from this post, is it relevant to you, and how will you modify the concepts taught in the post for your own business processes?

Make a post and tag me on Twitter @automationchamp using #AutomationChampionFlow.

Have feedback, suggestions for posts, or need more information about Salesforce online training offered by me? Say hello, and leave a message!

Preferred Timing(required)

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.

Discover more from Automation Champion

Subscribe now to keep reading and get access to the full archive.

Continue reading