• Nook 7
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 4
    Replies
Hello everyone! I need a code for trigger, that will insert all team members in a custom field Team_members__c in Opportunity.

I saw a lot of threads here, but nothing seems to work for me. I really hope someone could help, thanks!
Hello everyone! I need a code for trigger, that will insert all team members in a custom field Team_members__c in Opportunity.

I saw a lot of threads here, but nothing seems to work for me. I really hope someone could help, thanks!
Hi Community,

I have been trying to write a trigger that will update a field called Ad Ops Contact that I created on the Opportunity page. My first attempt was to write a trigger on Opportunity Team Member that updates the opportunity with the name of the person who is the Ad Ops contact but I could only get it to work when I actually updated the opportunity team and not when the opportunity team is added to the opportunity (which happens when the opportunity is created). So basically that trigger on the opportunityteammember worked on update but not on insert. So my second attempt after reading this link about related lists (https://developer.salesforce.com/forums/ForumsMain?state=id#!/feedtype=SINGLE_QUESTION_SEARCH_RESULT&id=906F0000000943LIAQ) was to write it on the opportunity itself. 
trigger updateAdOps on Opportunity (after update, after insert) {

    //Using set to be able to work with setOPP and setOTM sets
    set<Id> setOpp = new set<Id>();
    set<Id> setOTM = new set<Id>();
    
       //Loop through opportunity team members and grab users who have the role of 'Ad Ops'
    for (Opportunity oppTeam : trigger.new) { 
        setOpp.add(oppTeam.Id);
    } 
    
    
     //Create Map
    list<OpportunityTeamMember> lstOTM = new list<OpportunityTeamMember>([SELECT Id, UserId, OpportunityId, User.Name FROM OpportunityTeamMember WHERE Id in :setOTM AND (TeamMemberRole = 'Ad Ops') ]);

    Map<Id,Opportunity> mapOpps = new map<Id, Opportunity>([SELECT Id, Ad_Ops_Contact1__c FROM Opportunity Where Id IN :setOpp ]) ;          
    Opportunity tempOpp;
    
    
        //Load Values 
        for(OpportunityTeamMember otm : lstOTM ){
        tempOpp = mapOpps.get(otm.user.name); // Opportunity Team Member Id
        tempOpp.Ad_Ops_Contact1__c = otm.user.name; //otm.user.name is from user.name in the create map
    
    }
    insert mapOpps.values();  

}

I am getting an error that says Insert can't be done on this opperation. 

Any help would be appreciated.

Thanks,

Edward