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
Zach Gavin DelacroixZach Gavin Delacroix 

Opportunity contact role in Lightning not working in Trigger / Trigger Handler

Hello, Please help me get around an issue with Lightning.

When quering Opportunity, list.size() returns different value in Classic and Lighting. In Classic, it return right number and incorrect in Lightning.

This is how you can test it.

•Add the Trigger under Opportunity
trigger OpportunityTrigger on Opportunity (after insert) {
list<opportunitycontactrole> ocrList = [select id from opportunitycontactrole where opportunityid in : trigger.new];
system.debug(ocrList.size());
}
•Open Developer Console so you can see the Log Result.
•Add Opportunity from Contact's Opportunity Related List (In Classing Mode) and Check the Log.. The Result should be 1 in Classic which is Expected Result.
•Add Opportunity from Contact's Opportunity Related List (in Lightning Mode) and Check the Log. This part is where the Bug is as it returns 0 result..


NOTE: I've tried @Future call but still not working, I've tried to put it on Batch Apex and not working as well. You can notice that if you place the Code on Update and Update the Opportunity, you will see correct result but I need this on Insert. When you do Update after the record is inserted, then returns incorrect value again.

I need to get around this because I need to check if there are existing contact roles first before I insert a new one.

Thank you
 
jigarshahjigarshah
You could simply create a COUNT() Rollup Summary field that counts the number of existing Opportunity Contact Role records. Hence, you can check for existing Opportuntiy Contact Role records if that field > 0.
jigarshahjigarshah
Zach, is this issue resolved for you?
Zach Gavin DelacroixZach Gavin Delacroix
Thank @jigarshah,

However, Rollup summary is not possible for Contact Role in Opportunity. So this is not resolved