You need to sign in to do that
Don't have an account?
Edward Scott 5
Trigger on Opportunity to pull in Opportunity Team Members
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.
I am getting an error that says Insert can't be done on this opperation.
Any help would be appreciated.
Thanks,
Edward
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
That error is probably because I steered you wrong again.
Because it's and after update after insert trigger - you probably have to "re-aquire" the Opportunity records.
so - modify the trigger to be like this...
see if that works
All Answers
If that's the case - then you'll want to do an update to the oppty's. So - that's as simple as changing 20-26 with.... (I think)
But then you'll have another issue - as this trigger is then going to "fire" again - because you're doing an update. And it will continue to fire over and over - until you hit your SOQL limit (101 queries).
So, in this case - you'll want to use the "Run Once" technique. Different people do it different ways - but here is how I use it...
Hope that helps
If (util_utilities.alreadyExecuted != true) {
util_utilities.alreadyExecuted = true;
update mapOpps.values();
}
That way you update the values once, but not a second + time.
And the util_utilities.cls - all that is in there is the class defintion, and the property (alreadyExecuted) definition. It's a static variable - that's how it keeps it value from one call to the next.
I wish I would have thought of that at first.
You have a set of Opportunity IDs that fired the triggered already - so you can get all of the OpportunityTeamMembers for those opportunities with the first SOQL. Then put that list in a map of the OpportunityId, to the Ad Op UserID. Once you have that map populated - you can go through the triggered Opportunities and update the Ad_Ops_Contact1__c field.
You might want to wrap the o.Ad_Ops_Contact1__c = mOtms.get(o.id); with an If statement for the map. That would take care of any errors that might happen if there were NO Ad OPs at all. So like this...
if(mOtms.containsKey(o.id)) {
o.Ad_Ops_Contact1__c = mOtms.get(o.id);
}
However - I wanted to question you - what the Ad_Ops_Contact1__c field is? Is that a lookup to the CONTACT or to the USER? With the naming of the field - it makes me think it's pointing to the contact object. If that's the case - then you'll have another issue - as the OpportunityTeamMember is identified by the UserId.
good luck with it.
Jeff
The Ad_Ops_Contact1 is a text field that I was able to get to update when I was using the trigger on the opp team member. I switched it over though to a lookup field that goes to the user record. But now I am getting this error saying that the record is read only.
Should the field that I am updating be a look up to the user record?
I know that this is so close to working. Thanks again
That error is probably because I steered you wrong again.
Because it's and after update after insert trigger - you probably have to "re-aquire" the Opportunity records.
so - modify the trigger to be like this...
see if that works