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
nicksnicks 

Trigger On Opportunity to Count Opportunity Contact role from related list

Opportunity stage can be changed only when there's atleast 1 Opportunity contact Role(Related List). or else it will throw error "Add Contact Role".
-Trigger on Opportunity
AnkaiahAnkaiah (Salesforce Developers) 
Hi 

try with below code.
trigger preventstageName on Opportunity (before update) {


set<id> oppids = new set<id>();

for(Opportunity opp:trigger.new){

if(opp.StageName !=trigger.oldmap.get(opp.id).StageName){
oppids.add(opp.id);

}
}

List<opportunityContactRole> ocr = [SELECT id, Name from opportunityContactRole WHERE opportunityId IN:oppids];

for(opportunity opp:trigger.new){

if(ocr.size()<1){

opp.adderror('you can't change the status without contactrole for opportunity');

}

}


}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​