+ Start a Discussion
Zach Gavin DelacroixZach Gavin Delacroix 

Trigger now working in Lightning


I have an issue with a trigger now working in Lightning but works as expected in Classic.

I have a simple trigger which validates whether there's a contact role already in an Opportunity or not. If none, then will create one.

Example I have:
list<OpportunityContactRole> ocr = [select id from OpportunityContactRole where id in : oppList];
if there are 2 Contact Roles on the Opportunity the ocr.size() should return 2.. But on lightning, this always return 1..

The purpose of my trigger is when the Opportunity is created from the Opportunity Tab and account is selected, it will automatically create an Opportunity Contact Role based on the Contact on the Account.

However, when we do this under Contact, this automatically create 1 Contact Role (Standard SF function) and another 1 contact role from my trigger.

To avoid that, I figured I needed to do a validation before I create the Contact Role.

Here's my trigger.
public class SG_OpportunityTriggerHandler{
    //This will create Opportunity Contact Role
    public void NEW_OCR(list<Opportunity> OppList){
        map<id,OpportunityContactRole> ocrMap = new map<id,OpportunityContactRole>();
        for(OpportunityContactRole ocr : [select id,opportunityId,contactId  from OpportunityContactRole where opportunityId in : OppList]){
        list<OpportunityContactRole> newOCR = new list<OpportunityContactRole>();
        for(opportunity opp : [select id,account.Contact__c,account.Person_Account__c from opportunity where id in: OppList]){
            if(opp.account.Contact__c != null && opp.account.Person_Account__c == true){
                    OpportunityContactRole ocr = new OpportunityContactRole();
                    ocr.contactId = opp.account.Contact__c;
                    ocr.opportunityId = opp.id;
                    ocr.IsPrimary = true;


Paul S.Paul S.
Beginning with Winter '18, Salesforce is automatically creating an OCR when you add an opportunity from the contact related list.

Create Contact Roles for Opportunities from Contacts in Lightning Experience:
Create an opportunity and a contact role in one fell swoop. When sales reps create an opportunity from the opportunity related list on a contact, Salesforce adds the contact in a contact role on the opportunity. These changes apply to Lightning Experience and the Salesforce app only.

Is your trigger a before trigger?  If so, you might be able to work around this by switching to an after trigger and looking for the existence of the OCR before creating the new one.
Zach Gavin DelacroixZach Gavin Delacroix
Thanks for the reply @Paul

I'm aware that creating opportunity from contact related list will create OCR. That's what I'm trying to avoid, if opp is created from contact then do not create a Contact Role.

My Trigger is after insert.. That's why it's weird thing that it cannot determine 2 ocr.

Again, this trigger is working in Classic.

It seems that in Lightning, it cannot determine the 2 ocr. I even tried creating the ocr from trigger first and then had another method to query exisitng ocr on that opportunity but lightning returns 1 record only but 2 in classic.

so, I still need help on this one.. :(