You need to sign in to do that
Don't have an account?
Sarah 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 {
tmp_ocr.add(ocr);
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;
}
}
}
}
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 {
tmp_ocr.add(ocr);
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;
}
}
}
}
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.
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
Apologies,I forgot that
- We can’t add any custom fields into OpportunityContactRole Object.
- We can’t add any validation rules on OpportunityContactRole object.
- 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.
You can Create a custom Vf page and set that page on Opportunity Page Layout to acheive your Output.
See this....
http://www.soliantconsulting.com/blog/2012/08/view-triggers-salesforce-trigger-opportunity-contact-roles
This will help you to get your output.