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
Peter CowenPeter Cowen 

count number of records in a related list

I have two objects case and Service Request. I would like to have a field which counts the number of cases attached to the Service Request.

I am unable to have a rollup field.

Ideally i would like to have a graph that shows the number of cases and when the number increases (like a time line 9am 3 cases 10am 5 cases)
but just a field would do to count all cases added.

Does anyone know how to do this?
JyothsnaJyothsna (Salesforce Developers) 
Hi Peter,

Please check the below sample code and replace the API names.

The trigger on Contact object.When contact is "created No_of_contacts__c" custom field in account increment by 1.
trigger ContactCount on Contact (after insert, after update, after delete) {
    Map<Id, List<Contact>> AcctContactList = new Map<Id, List<Contact>>();
    Set<Id> AcctIds = new Set<Id>();    
    List<Account> AcctList = new List<Account>();
    List<Contact> ConList = new List<Contact>();
    if(trigger.isInsert || trigger.isUPdate) {
        for(Contact Con : trigger.New) {
    if(trigger.isDelete || trigger.isUPdate) {
        for(Contact Con : trigger.Old) {
    if(AcctIds.size() > 0){
        ConList = [SELECT Id, AccountId FROM Contact WHERE AccountId IN : AcctIds];
        for(Contact Con : ConList) {
                AcctContactList.put(Con.AccountId, new List<Contact>());
        System.debug('Account Id and Contact List Map is ' + AcctContactList);
        AcctList = [SELECT No_of_contacts__c FROM Account WHERE Id IN : AcctIds];
        for(Account Acc : AcctList) {
            List<Contact> ContList = new List<Contact>();
            ContList = AcctContactList.get(Acc.Id);
            Acc.No_of_contacts__c= ContList.size();
        System.debug('Account List is ' + AcctList);
        update AcctList;    


Hope this helps you!
Best Regards,
Peter CowenPeter Cowen
I have wrote a bit of the trigger and it appears to have some errors. I have commented the errors trigger count on Case (after insert, after update, after delete) { Map> CaseList = new Map>(); Set CaseIds = new Set(); //declare Variables Id RequestRecordTypeId = [Select id from RecordType where sObjectType = 'Service_Request__c' and developerName ='Incident' ].id; Id CaseRecordTypeId = [Select id from RecordType where sObjectType = 'Case' and developerName ='Service_Desk' ].id; Id ServiceRequestId = [select Id from Service_Request__c where RecordTypeId =: CaseRecordTypeId]; //Variable does not exist List CasesList = new List(); //Duplicate List RequestList = new List(); String var = 'TFS_Number__c'; String var2 = 'ServiceRequestName__c'; //Duplicate - Recreation Casebugnumber = [select TFS_Number__c from Case where RecordTypeId =: CaseRecordTypeId ] //not completed if (trigger.isInsert || trigger.isUPdate){ //unexpected if Statement for (Case Con : trigger.New){ if (string.isNotBlank(TFS_Number__c)) //Variable does not exist if (string.isblank(ServiceRequestName__c)){ try { ServiceRequestName__c = [select Id from Service_Request__c where TFS_Number__c =: Casebugnumber]; CaseIds.add(case.ServiceRequestName__c); } } } if (trigger.isDelete || trigger.isUpdate){ for (Case Con : trigger.old){ CaseIds.add(ServiceRequest); } } } Can anyone advise on where I have gone wrong and also how I could check if the CaseBugnumber = ServiceRequestBugNumber