To achieve this requirement using just your current skill set, you're going to have to live with a compromise. Because of the inability to add an
Apex Trigger to
OpportunityContactRole, any rollup calculations will have to be scheduled, meaning they'll run daily.
I'll copy my recommendation from the comments:
If you're open to using DLRS, it would be simple enough to roll up the count and just validate that the result field equals zero and join that with your picklist condition.
It sounds like you know where to take it from there. That's your best option until you work your way through several Trailhead modules and master the topics of:
Once you master those topics, you will be able to write a validation using
Apex, specifically the
SObject.addError method. For what it's worth, your service method to filter records which have an OCR could look something like:
public with sharing class OpportunityService
// @param records - pass in trigger.new
public static List<Opportunity> getRecordsWithOcr(List<Opportunity> records)
List<Opportunity> recordsWithOcr = new List<Opportunity>();
Set<Id> opportunityIdsWithOcr = new Map<Id, AggregateResult>([
SELECT OpportunityId Id FROM OpportunityContactRole
for (Opportunity record : records)