Monday 11 January 2016

How to Invoke Opportunity Trigger when Opportunity Contact Role Created ?

As we already know Opportunity and contact are related through the junction object OpportunityContactRole and when OpportunityContRole created there wont be any update event to Opportunity. In that case how will we invoke opportunity trigger ?

How will we perform some action when an opportunity is linked to contact?

Possibly answer would be via workflow or trigger. Unfortunately we wont to able to write trigger on OpportunityContactRole nor workflow.

Below is workaround for that scenario.

  • Create  Visual-force page.
  • Create a temp field on Opportunity.
  • Add that vf page on opportunity layout.
  • Set height and width as zero so that it wont be displayed on opportunity layout. 
After OpportunityConRole created, it returns back to Opportunity layout that means inline vf page will be loaded by which one action method will be invoked to update temp field on opportunity.

Once that field is updated, obliviously Opportunity Trigger will be executed.
 

<apex:page standardController="Opportunity" extensions="OppTriggerInvoked" action="{!updateoppty}" />

public with sharing class OppTriggerInvoked {
    public Opportunity opp;
    public OppHelper( ApexPages.StandardController stdController ) {
        opp = ( Opportunity )stdController.getRecord();        
    }
    public void updateoppty(){
        OppTriggerInvoked .updateopptyRecord( opp.Id );    
    }
    @future public static void updateopptyRecord( Id oppId ) {
        Opportunity opp = new Opportunity (id = oppId );
        opp.Is_Updated__c =  true;
        update opp;
    }
}

2 comments:

Shahid Ali said...

This doesn't works with Lightening and throws an collision detection error message - "This record was modified by Your-Own-User during your edit session" if you update any opportunity field. Please let me know if you have any workarounds.

Anonymous said...

There is an update from salesforce that we can create apex trigger on Opportunity Contact Role in spring 20 release.