function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

How to update opportunity if contact role inserted ?

Good day, 


I trying to create a trigger which intend to update oppty field if contact role are inserted, and noticed that we can't create a trigger for OpportunityContactRole object, and worst, the opportunity trigger seem do not trigger on update when new contact role record are created, but noticed lastmodifiedDate is changed on opportunity.


I would appreciated if someone point out if i wrong in understanding this limitation and possible state any mistake of the trigger as well (I'm newbie of SFDC)


trigger MycontactRoleTrigger on Opportunity (before update) {
List<OpportunityContactRole> opptyContactRoleList= [SELECT, 
ContactId, Id, CreatedById, CreatedDate, IsDeleted, LastModifiedById, LastModifiedDate, 
OpportunityId, IsPrimary, Role, SystemModstamp FROM OpportunityContactRole 
WHERE OpportunityId IN :trigger.newmap.keyset() ];
System.debug('opptyContactRoleList list = '+opptyContactRoleList);									

			for(Opportunity oppty :{
				if(al.StringUtils.isBlank(oppty.contactPersonName__C) )){
					for(OpportunityContactRole ocr :opptyContactRoleList){
							oppty.contactPersonName__C 	 =;							



Create Custom Object(Replica of OpportunityContactRole) and then write a trigger to update the opportunity on Contact Role insertion.

Hope this way you can achieve the desired functionality .


How are we going to replicate it ? for every new record of OpptyContactRole, we will insert into new object ? wow ....i think it is expensive to do that


This is the way you can make it work...

It's impossible to create the trigger for OpportunityContactRole object.As you cannot create trigger on any contact role object so you can have custom object for the same and writer trigger to update opportunity field.



Hi UV,


I want to fire a trigger on insertion of Contact Role. But, unfortunately its not possible.

Can you please explain the custom object workaround?




You would need to create the replica of contactrole relationship using custom object and then you can achive what you are looking for..First see the behaviour of contactRole object and try to replicate the same using custom object.Once this is done, you can fire a trigger on that new custom object.


Thanks UV. So, basically I need to create 2 master detail relationships(Contact and Opportunity) and role picklist field in newly created object. Is there anything that I need to do?


We can do this without creating anything new objects or fields etc.


You just need an in-line VF page on Opportunity Detail page. Basically whenver you add/edit new Opportunity Contact role it will load the Oppty detail page back.


Instead of show the field directly on the layout, show the field name and value in the inline VF page and also in the page's action method query the Opportunity Contact roles and update the relevant field on Opportunity.


Here is some sample code.

<apex:page standardController="Opportunity" extensions="VfOppContactRoleController" action="{!updatePrimaryContact}">
	<apex:outputLabel value="Primary Contact" for="pc" styleClass="labelColumn"/> <apex:outputField Id="pc" value="{!Opportunity.Primary_Contact__c}" />


public with sharing class VfOppContactRoleController 
	public string oppId {get; set;}
	public VfOppContactRoleController(ApexPages.StandardController oppController)
		oppId = oppController.getId();
	public void updatePrimaryContact()
		Opportunity oppToUpdate;
		for(OpportunityContactRole ocr : [select ContactId from OpportunityContactRole where OpportunityId = :oppId and isPrimary = true])
				oppToUpdate = new Opportunity(Id = oppId, Primary_Contact__c = ocr.ContactId);
		if(oppToUpdate != null)
			update oppToUpdate;


Nirmal ChristopherNirmal Christopher

trigger trigMapFields on Lead (before update) {
for(Lead {
if (lead.IsConverted) {
// Assign the value from the Lead "Status" field to the Contact "Type" field
Contact con = [SELECT Id FROM Contact WHERE Contact.Id = :lead.ConvertedContactId];
Opportunity opp = [SELECT Id FROM Opportunity WHERE Id = :lead.ConvertedOpportunityId];
update con;



i had similar requirement this simple trigger will do the job but this trigger will work only for converted opportunity ield updates.


This was hugely helpful, and I ended up using your solution. However, it updates the visualforce page, but it does not update the field on the opportunity that I need it to update until the opportunity is edited. I can add the contact role, but the opportunity won't recognize it unless I then hit edit and save on the opportunity (and then it pulls the value into the field that's actually on the opportunity layout rather than the visualforce page). Is there any way to fix this? Either to have it write all the way up to the opportunity or to refresh the opportunity after the value rolls up to the visualforce page?


Thank you!