Monday, March 26, 2018

Batch apex class to update a field 

The requirement is i need to write a Batch Apex Class to update Case status field to close when related tickets field status is closed. The update of status should be after 7 days of all the tickets related to a case are closed.
There are two objects who appropriate fields are
  • Case (Object) : status (Field)
  • Ticket__C (Object) : GLOBAL_Ticket_Status__c (Field).
Note: Ticket__c Has a look up to the Case. So, a case can have multiple tickets but a ticket can only be associated to one case.
The class i have written should do the following steps
  1. Get all the records of a case whose status is Open, Breached and Escalated.
  2. List all the tickets associated to a case i.e CaseId = Ticket.Id
  3. check for all Ticket__c associated to case whose status = closed (if the status of ticket != closed,come out of the loop. Because a case cannot be closed when any of its tickets status #!= closed)
  4. if all the tickets of a case are closed, update status of a case to close after 7 days of ticket status == close using lastmodifielddate.
Below is the code i could write so far. I need help in modifying the code to make it work
global class CloseCaseBatch implements Database.Batchable<sObject>, Schedulable{
  private Boolean allClosedTickets;
  private String query = 'Select Id, Status from Case WHERE Status = \'Open\' OR ' +
         'Status = \'Escalated\' OR' +
         'Status = \'Breached\''; // Getting case records whose status is open (or) escalated (or) Breached

  // method implemented from Schedulable interface
  global void execute(SchedulableContext sc){
    Database.executebatch(this); 
    }
  //Start Method which retrieves the records of case
  global Database.QueryLocator start(Database.BatchableContext BC)  {
      return Database.getQueryLocator(query);
         System.debug('checkcase---' + query); //debugging
      }
  global void execute(Database.BatchableContext BC, List<sObject> scope) {
    List<Case> casestatusOEB = (List<Case>)scope; //List of cases
    System.debug('checkcase---' +scope); //debugging
    Set<ID> caseIDs = new Set<ID>(); // Setting Case id
    Map<ID,List<Ticket__c>> CaseID_TicketList = new Map<ID,List<Ticket__c>>(); //mapping case id to ticket__C
      for(case getCaserecords : casestatusOEB) {
             caseID.put(getCaserecords.id);  // Setting the case ID 
      }

  }
  List<Ticket__c> ticket = [Select ID, LastModifiedDate , GLOBAL_Ticket_Status__c,
                             GLOBAL_Goal__c from Ticket__c 
                             where GLOBAL_Goal__c in :caseIDs]; // Listing tickets whose id = Caseid
      for(Ticket__c tkt : ticket) {
      System.debug('new list of ticket' +ticket); 
          if (tkt.GLOBAL_Ticket_Status__c == 'closed')
              caseIDs.add(ticket.GLOBAL_Goal__c);    
      } 

  List<Ticket__c> tkt = new List<Ticket__c>();
      if(CaseID_TicketList.containsKey(ticket.GLOBAL_Goal__c)){
          tkt = CaseID_TicketList.get(ticket.GLOBAL_Goal__c);
      } 
          else {
          tkt.add(ticket);                }
          CaseID_TicketList.put(ticket.GLOBAL_Goal__c,tkList);
          // Map case & ticket
          }


  allClosedTickets;  // validation to check ticket status. if status != closed come out of the loop 
      for (Ticket__c ticket: scase) { // Ticket loop
         System.debug('checkcase---' +ticket); //debugging
      // save LastModified date of the highest date
          if (ticket.GLOBAL_Ticket_Status__c != 'closed') {
             allClosedTickets = false;
          //caseIDs.add(ticket.GLOBAL_Goal__c);   // case ID of current Records.
          }

    }

          if (allClosedTickets = true) { // all closed so Case can be closed
          // if lastModifiedDate greater than 7 days then
          // add CaseId to casesToUpdate


  List<Case> casesToUpdate = new List<Case>();  // getting total tickets of that case and closedticket and if(closedTickets == totalTickets) and all of them are closed. then update the case status to closed after 7 days of a tickets status = closed based on lastmodifieddate >7

      for (Id caseID : caseIDs) {

          Integer totalTickets = CaseID_TicketList.get(caseID).size();
          Integer closedTickets = 0;
              for(Ticket__c ticket : CaseID_TicketList.get(caseID)){ 
                  if(ticket.GLOBAL_Ticket_Status__c == 'Closed'){
                  closedTickets++;
                  }
              }
                  if(closedTickets == totalTickets) {
                  casesToUpdate.add(new Case(Id = caseID, Status= 'Closed'));
                  }
              }

                  if(!casesToUpdate.isEmpty()) {
                  update casesToUpdate; //updating case status after 7 days"" 7 days logic is missing
                  }
      }

  global void finish(Database.BatchableContext BC)
    {

    }

  }

No comments:

Post a Comment