+ Start a Discussion
Edward Scott 5Edward Scott 5 

Opportunity Team member on Opportunity


I am trying to wirte a trigger that will pull the Ad ops team member from opportunity teams to the opportunity page. I found code in the forum that I was able to tweak and make work but it the trigger is on the opporunityteam so it only updates the field when I edit the opportunity team. Since our opportunity teams are inserted automatically when the opportunity is created this is a problem. I was wondering if anyone has already solved this problem. 

Thanks in advance for your help,

Hi Edward, perhaps you can set your trigger on the opportunityteam object to fire in an after insert context? If you haven't already I'd also recommend you extract the logic to a helper class to make future changes easier to handle.
Can you please include the code (and the forum post that you got it from) here so we can see what you have?

Note: When adding code use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
Edward Scott 5Edward Scott 5
trigger InsertOppTeam on OpportunityTeamMember (before insert, after update) {

    //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 (OpportunityTeamMember oppTeam : trigger.new) { 
    //Create Map
    list<OpportunityTeamMember> lstOTM = new list<OpportunityTeamMember>([SELECT Id, UserId, OpportunityId, User.Name FROM OpportunityTeamMember WHERE Id in :setOTM AND (TeamMemberRole = 'Ad Ops'OR TeamMemberRole = 'Inside Sales Support') ]);

    Map<Id,Opportunity> mapOpps = new map<Id, Opportunity>([SELECT Id FROM Opportunity Where Id = :setOpp ]) ;          
    Opportunity tempOpp;
        //Load Values 
        for(OpportunityTeamMember otm : lstOTM ){
        tempOpp = mapOpps.get(otm.OpportunityId);
        tempOpp.Ad_Ops_Contact1__c = otm.user.name;
    update mapOpps.values();  

This is the code its a trigger on the opportunity team and this is the link to the post in the community https://developer.salesforce.com/forums/ForumsMain?id=906F00000005IRF#!/feedtype=SINGLE_QUESTION_SEARCH_RESULT&id=906F0000000943LIAQ. This updates a field that I created on the opportunity called Ad_Ops_Contact1 but it only does it after I alter the op team and not when I create the opportunity. Thanks for your help
Hi Edward,

I believe you are seeing this issue because when the trigger fires in the before insert context the OpportunityTeamMember record(s) does/do not yet have an id, which will cause problems with your SOQL query in line 14. I also think your SOQL query on line 16 should read 'WHERE Id IN :setOpp"
Edward Scott 5Edward Scott 5
Hey Scott,

I made the changes to the code but it will still only fire after I actually update the opportunity team and not the opportunity.

Thanks again for your help.
Hi Edward,

Just want to be sure I understand your flow properly: an after insert opportunity trigger inserts one or more opportunity team member records. You also have an after insert trigger on the opportunityteammember object that should update a field on the parent opportunity record, but this isn't working. This trigger also runs in the after update context and if you edit>save an opportunityteammember record then the trigger does fire as expected? Although this should affect the logic no matter the context I did find a few other changes you might want to make to your code. First, sometimes there can be issues when you attempt to update a field for a record pulled from a SOQL query that didn't include said field. So I would change your soql query on line 16 to include the Id field and the Ad_Ops_Contact1__c field. You also might want to add code below line 22 to replace the mapOpps value with the updated opportunity record like so:
Edward Scott 5Edward Scott 5
Yup you have the workflow correct. I can get the trigger to fire when I update the opportunity team but I can't get it to work when the opportunity team is inserted on the opportunity (which happens when the opportunity is created). Do you think it is because the insertion action is actually happening on the opportunity when the opportunity team is being added in?

I would put some System.debug statements into your trigger to flush out the problem. You might want to debug key variable values such as the parent opportunity id from the opportunityteammember trigger. Then you can follow yourself in the Salesforce Debug Log and take a peek to get a better idea of what's going wrong.