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
viswsanathviswsanath 

How to count the number of records on opportunity associated with on Account ?

HI ,

How to count the number of  records on opportunity  associated with on Account ? pls give me exammple ,
kindly help me on this 

viswsanath
Amit Chaudhary 8Amit Chaudhary 8
You can create the rollup summery field on Account to count the record
Sudipta DebSudipta Deb
Hi Viswsanath,

You need to create roll-up summary field to achieve the requirements. 
Go through the below documents to understand how to create roll-up summary field  Please accept my solution as Best Answer if my reply is helpful.
Mahesh K 22Mahesh K 22
Hi  viswsanath


trigger Rolluptriggroncontact on Contact (after insert,after update,after delete,after undelete) {
   set<id> setlst = new set<id>();
    if(trigger.isinsert||trigger.isupdate||trigger.isundelete){
    for(contact c :trigger.new){
        if(c.AccountId != null)
            setlst.add(c.AccountId);
        
    }
    }
    if(trigger.isupdate||trigger.isdelete){
        for(contact oldcon :trigger.old){
            if(oldcon.AccountId != null)
                setlst.add(oldcon.AccountId);
        }
    }
    if(setlst.size()>0){
        List<account> acclst =[select id,No_of_Contacts_in_SFDC__c,(select id from contacts) from account where id in :setlst];
        list<account> updaterec = new list<account>();
        for(account acc : acclst){
            acc.No_of_Contacts_in_SFDC__c =acc.contacts.size();
            updaterec.add(acc);
            
        }
        update updaterec; 
    }
}


simple change instead of contact to opportunity