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
Kanika Dua 1Kanika Dua 1 

Counting Total Number of Cases

Hi All,
I am newbiee to Salesforce.I have two Objects Case and a custom Object CaseReplica.On Case Replica I have a custom Field Total Cases.What I want to do is count Total number of cases created in a day  and save that number in field Total Cases in CaseReplica.One Record For a day should be created in CaseReplica.Please suggest me methods to do it.
Thanks in advance 
Best Answer chosen by Kanika Dua 1
DebasisDebasis
Hi Kanika,

you can check this sample trigger for updating realtime count of cases in CaseReplica__c .This trigger handels both insert and delete of case record.
if you insert a new case then TotalCase__c in CaseReplica__c will  updated with new count and if you delete a case then count will decrese also.
 
trigger CaseTrigger on case(after insert,after delete){
//total num of case rec in execution
	integer noOfRecInCase = Trigger.size;
//fetch case_replica__c record to update if exists
	caseReplica__c replica = [select id,TotalCase__c from CaseReplica__c where createdDate=Today limit 1]
//logic for inserting new case, update case replica count
	if(trigger.isinsert){
		
		if(replica!=null){
			replica.TotalCase__c = replica.TotalCase__c+noOfRecInCase;
			//update replica; 
		}
		else{//if replica record not exists then create new one
			 replica = new caseReplica__c();
			replica.TotalCase__c=noOfRecInCase;
			//insert replica;
		}
		
	}
//logic for deleteing case, decrease case count in replica
	if(trigger.isdelete){
		if(replica!=null){
			replica.TotalCase__c = replica.TotalCase__c - noOfRecInCase;
			//update replica; 
		}
		
	}
	if(replica!=null){
		upsert replica;
	}
}

please do let me know if it helps you


Thanks,
Debasis

All Answers

Ivan GrgurinaIvan Grgurina
Hi Kanika,

the easiest way to count related records is to make a Master-Detail connection between them and then use the aggregate Formula field to count related detail records on your master object. You do that by adding a new custom field of type Master-Detail Relationship.

To create a new record every day, use a scheduled job.
Kanika Dua 1Kanika Dua 1
@Ivan
I want to count Cases.And We Can not make Case as a detail in Master Detail Relationship
Ivan GrgurinaIvan Grgurina

@Kanika,

Then make a trigger that on every Case insert adds 1 to the Total Cases field in CaseReplica.

Or create a scheduled job that runs at the end of the day and goes over all Cases created that day and saves their count to the Total Cases field in CaseReplica. Something like this:

SELECT Count() FROM Case WHERE DateCreated = Date.Today()

I prefer the second option.

DebasisDebasis
Hi Kanika,

Use a scheduler apex for this and schedule it for everyday night after 12am. I have written  below  sample scheduled class  for this.
global class DailyCountCase implements Schedulable {
   global void execute(SchedulableContext ctx) {
	//this wil give total number of case record created yesterday
// using yesterday as this job will be scheduled after 12am so that it will take count of all record of previuos day	
      integer caseCount = [SELECT count() from case where createddate=YESTERDAY]
	  CaseReplica__c replica = new CaseReplica();
	  //populate other fields if you have
	  replica.TotalCase__c = caseCount;
	  insert replica;
   }   
}
please scheduled this class to run everyday at 1am as per below image.
User-added image
 
please do let me know if it helps you.


Thanks,
Debasis
Kanika Dua 1Kanika Dua 1
@Debasis @Ivan
Thanks For Responding.But Solution of Scheduled Job Seems to be Static as it will Execute at 1 AM.I want to see real Time data.ie. For every new Case record of CaseReplica is Updated.How I can accomplish that.Can you please Help me with syntax for Trigger and On which object Trigger should be Created on Case or CaseReplica?
Thanks in advance
 
DebasisDebasis
Hi Kanika,

you can check this sample trigger for updating realtime count of cases in CaseReplica__c .This trigger handels both insert and delete of case record.
if you insert a new case then TotalCase__c in CaseReplica__c will  updated with new count and if you delete a case then count will decrese also.
 
trigger CaseTrigger on case(after insert,after delete){
//total num of case rec in execution
	integer noOfRecInCase = Trigger.size;
//fetch case_replica__c record to update if exists
	caseReplica__c replica = [select id,TotalCase__c from CaseReplica__c where createdDate=Today limit 1]
//logic for inserting new case, update case replica count
	if(trigger.isinsert){
		
		if(replica!=null){
			replica.TotalCase__c = replica.TotalCase__c+noOfRecInCase;
			//update replica; 
		}
		else{//if replica record not exists then create new one
			 replica = new caseReplica__c();
			replica.TotalCase__c=noOfRecInCase;
			//insert replica;
		}
		
	}
//logic for deleteing case, decrease case count in replica
	if(trigger.isdelete){
		if(replica!=null){
			replica.TotalCase__c = replica.TotalCase__c - noOfRecInCase;
			//update replica; 
		}
		
	}
	if(replica!=null){
		upsert replica;
	}
}

please do let me know if it helps you


Thanks,
Debasis
This was selected as the best answer
DebasisDebasis
Hi Kanika,
please let me know if you need any further assistance or any of above answer hemos you to resolve your issue.

Thanks,
Debasis
Kanika Dua 1Kanika Dua 1
@Debasis Thanks It worked Out for me