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
sourabh kalvasourabh kalva 

Write a trigger to count the number of opportunities that are still open under an account.

sfdc98sfdc98
Hi try below trigger..

trigger countopprec on Opportunity (After insert,After Delete,After Undelete) {
set<id>accountIds=new set<id>();
    if(trigger.isInsert || trigger.isUndelete){
        for(opportunity o:trigger.new){
            accountIds.add(o.AccountId);
        }
    }
    if(trigger.isDelete){
        for(opportunity o:trigger.old){
            accountIds.add(o.Accountid);
        }
    }
    List<Account>acclist=[select id,name,No_of_opportunities__c,(select id,stageName,closeDate,name from opportunities)from Account where id=:accountids];
    for(Account a :acclist){
       a.No_of_opportunities__c=a.opportunities.size();
    }
    update acclist;
}

mark as best answer if it helps,
Thanks
SwethaSwetha (Salesforce Developers) 
HI Sourabh,
You can tweak the solution provided in https://salesforce.stackexchange.com/a/206670/80089 as per your requirement 

If this information helps, please mark the answer as best. Thank you