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
siree gantisiree ganti 

Batch Apex Class to calculate average year-to-date, average month-to-date, and daily total

Hi All,
The following code doesn't seem to work.  Trying to calculate daily total for support cases closed, average month-to-date (mtd), average year-to-date ytd for reporting purposes.  Please help!!
here's the code:

global class batchA0806 implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        Integer DailyTotal = SELECT COUNT() FROM Cases where ClosedDate=TODAY ;
        Integer a = SELECT COUNT() FROM Cases where ClosedDate=THIS_MONTH;
        Integer b = SELECT COUNT() FROM Cases where ClosedDate=THIS_YEAR;
        return Database.getQueryLocator(query1);
    return Database.getQueryLocator(a);
    return Database.getQueryLocator(b);
    }
   
    global void execute(Database.BatchableContext BC)
    {
         
             mtd = a/THIS_MONT;
         ytd = b/THIS_YEAR;
             update mtd;
             update ytd;
             update DailyTotal;
    }   
    global void finish(Database.BatchableContext BC)
    {
    }
}
ManojjenaManojjena
Hi Siree,

Can you please expalin your requirment bit detail like what exactly you want .I don't think you need batch apex for this .
1.Like in which object you need to update the case details .
2.When you want to execute this class .As you are going to calculate closed cases or cases this Year  THis Month and Today. So where you need to update the count .

Don't you need the previuos month and yesterday count as well .

Thanks
Manoj


 
siree gantisiree ganti
Hi Manoj,
The requirement is to show a dashboard component with Daily Total for # of closed cases, Average month to date, and Average year to date for # of closed cases.  This component must be refreshed daily. In order to do a dashboard component, I need a report.  But I cannot do the above calculations within the report formula fields. Therefore, I'm doing a batch class on a custom object with three custom fields i.e., DailyTotal, AvgMtd, AvgYtd.  Makes sense?
Siree
ManojjenaManojjena

Hi siree ganti,

It's ok ,correct me if I am wrong. You need to create one record in your custom object with three values .So instead of batch apex you can do one thing just create a schedule apex class .
In execute method you can do all three query to get the value and create one record of your custom object .and schedule that class as per your need .
 
global class scheduledClass implements Schedulable {
   global void execute(SchedulableContext sc) {
        Integer DailyTotal = SELECT COUNT() FROM Cases WHERE  ClosedDate=TODAY AND status='Closed' ;
		Integer monthTotal = SELECT COUNT() FROM Cases where ClosedDate=THIS_MONTH AND status='Closed';
		Integer YearTotal = SELECT COUNT() FROM Cases where ClosedDate=THIS_YEAR AND status='Closed';
		Custom_Object__c obj=new Custom_Object__c();
		obj.DailyTotal__c=DailyTotal;
		obj.AvgMtd__c=monthTotal;
		obj.AvgYtd__c=YearTotal;
		try{
			Insert obj;
		}catch(DmlException de ){
		  System.debug(de);
		}
    }
}

Any calculation you need you can do inside excute method .Let m eknow if it helps .
siree gantisiree ganti
Hi Manoj, I'll try this out.  Thank you for the input :)

Siree