+ Start a Discussion
Sarah DallimoreSarah Dallimore 

Apex Trigger to Update Lookup field on Opportunity from Contact role

Hi - I am a bit of a novice when it comes to triggers, any help on where I have gone wtrong would be great.

I am trying to update a custom lookup field on the Opportunity called Participant_Name__c with the Primary Contact on the Contact Role object. Below is the code I have used (which I have copied and pasted from a forum and amended with the custom field name I have set up. I just tested it... nothing happens. Any idea what could be wrong? Thanks in advance

trigger ParticipantName on Opportunity (before insert, before update) {

Set<Id> OppIds = new Set<Id>();
for (Opportunity o : Trigger.new) {OppIds.add(o.id);}

Map<Id, List<OpportunityContactRole>> Opp_OCR = new Map<Id, List<OpportunityContactRole>>();

for (OpportunityContactRole ocr: [select id, ContactId, Opportunityid, role, isPrimary from 
OpportunityContactRole where opportunityid in :OppIds and isPrimary = true]) {

List<OpportunityContactRole> tmp_ocr = new List<OpportunityContactRole>();
tmp_ocr = Opp_OCR.get(ocr.opportunityid);
if (tmp_ocr == null) {
Opp_ocr.put(ocr.opportunityid, new List<OpportunityContactRole>{ocr});
} else {

Opp_ocr.put(ocr.opportunityid, tmp_ocr);

system.debug('Final OCR map: '+Opp_OCR);

for (Opportunity opps : Trigger.new) {
List<OpportunityContactRole> this_OCR = new List<OpportunityContactRole>();
this_ocr = Opp_ocr.get(opps.id);
system.debug('this Opps ('+opps.id+') list of OCRs: '+this_ocr);

if (this_ocr == null) opps.Participant_Name__c = null;
else {

for (OpportunityContactRole r : this_ocr) {
if (r.isprimary) opps.Participant_Name__c = r.ContactId;

Dushyant SonwarDushyant Sonwar
Hey Sarah,
Are you trying to change primary contact and it updates the participant name.If this is your end requirement then your trigger will be on OpportunityContactRole not the other way round as you have done.
Hope this helps.
Sarah DallimoreSarah Dallimore
Hi Dushyant - that is correct, as our sales rep's always create an Opp from within the contact record so the contact role is added automatically however I then want it to populate the participant name field on the Opportunity.

I have amended the trigger so that it says trigger ParticipantName on OpportunityContactRole (before insert, before update) { (if this is the part you mean I should change) and it says Error: Compile Error: SObject type does not allow triggers: OpportunityContactRole at line 1 column 28

Am I not changing it in the correct place? Thanks for your help
Dushyant SonwarDushyant Sonwar
Hi Sarah,
Apologies,I forgot that
  1. We can’t add any custom fields into OpportunityContactRole Object.
  2. We can’t add any validation rules on OpportunityContactRole object.
  3. We can’t create any trigger on this OpportunityContactRole Object.
you can acheive your output using  Apex Schedule Class which checks for every interval of time.
Hope this helps.
Dushyant SonwarDushyant Sonwar
Hey Sarah,
You can Create a custom Vf page and set that page on Opportunity Page Layout to acheive your Output.
See this....
This will help you to get your output.