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
Chinu_SFDCChinu_SFDC 

can someone please explain me the trigger to - "Add Opportunity owner to sales team when a new Opportunity is created"

Let say a new Opportunity is created by Rohit... As soon as a new opportunity is created by Rohit, I want to add him to sales team.

Please help me in explaining the code !
Ajay K DubediAjay K Dubedi
Hi,

First you need to create Opportunity Team Selling so go to below step:

Click Your Name | Setup | Customize | Opportunities | Opportunity Team Selling.
Click the Enable Team Selling or Disable Team Selling link.
Select or deselect the Team Selling Enabled checkbox and click Save. Enabling team selling gives your users access to create and use sales teams on opportunities.
If you are enabling team selling, select the opportunity page layouts that should include the new Sales Team related list and click Save.

Use below code:
// Trigger Code
trigger triggerOnOpportunity on Opportunity (after insert) {
    if(trigger.isInsert && trigger.isAfter){
        CreateUserAsSalesTeamMember.CreateUserAsSalesTeamMember_method(trigger.new);
    }
}

// Class code
public class CreateUserAsSalesTeamMember {
    public static void CreateUserAsSalesTeamMember_method(List<Opportunity> opportunityList){
        List<OpportunityTeamMember> otmList = new List<OpportunityTeamMember>();
        for(Opportunity oppObj : opportunityList){
            OpportunityTeamMember otmObj = new OpportunityTeamMember();
            otmObj.UserId = oppObj.OwnerId;
            otmObj.OpportunityId= oppObj.Id;
            otmList.add(otmObj);
        }
        upsert otmList;
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi