Sunday 7 May 2017

Summarize Case Total:

Rollup child to parent using Batch Class:

global class SummarizeCaseTotal implements Database.Batchable<sObject>, Database.Stateful{

   global final String query;
   global Map<Id, Case> casemap;
  
   global SummarizeCaseTotal(){
   casemap = new Map<Id, Case> ();
   }
 global Database.QueryLocator start(Database.BatchableContext BC){
      String query = 'select id, Duration__c, whatID from task';
      return Database.getQueryLocator(query);
   }
global void execute(Database.BatchableContext BC, List<sObject> scope){
   
   List<Task> ops = (List<Task>)scope;
   
   for (Task T : ops) 
{
    if(t.Duration__c != null)
    {
        if(casemap.containsKey(t.whatId))
        {
            Case c = caseMap.get(t.whatId);
            c.Billable_Hours__c += t.Duration__c;
            System.debug('#####################'+t.Duration__c);
            System.debug('$$$$$$$$$$$$$'+c.Billable_Hours__c);
            caseMap.put(t.whatId, c);
        }
        else
        {
            casemap.put(t.whatId, new Case (Id = t.whatID, Billable_Hours__c = t.Duration__c));
        }
    }
}
}
global void finish(Database.BatchableContext BC){
   
   try {
   update casemap.values();
   }
   catch (Exception Ex) {
   system.debug(Ex);
   }
   
   }
}


Scheduling the Batch Class:


global class scheduleSummarizeCaseTotal implements Schedulable {
   global void execute(SchedulableContext SC) {
      SummarizeCaseTotal sct = new SummarizeCaseTotal();
      database.executebatch(sct); 
   }
}


Schedule every 1 hour

System.schedule('Summarize Case Total', '0 0 0/1 * * ?', new scheduleSummarizeCaseTotal());

No comments:

Post a Comment

Batch Apex

1. What are transaction limits in apex? Total number of SOQL queries issued1 - 100 Total number of records retrieved by SOQL queries - 50...