I need to write a Scheduled Batch Apex Class on two Objects Case and Ticket__c.

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.

What the Scheduled Batch Class should do:

****** Run the batch apex at 2:00am every Morning

  1. Get all the records of a case whose status is Open, Breached and Escalated.
  2. List all the tickets whose Id = Case.Id
  3. Loop through to validate the Ticket_c status (GLOBAL_Ticket_Status__c=Closed) // if not come out of the loop
  4. Add the tickets id to case id and close the case after 7 days of Highest LastModifiesDate of a ticket associated to case i.e Ticket__c (Status=close) and Highster Lastmodified > 7 close the case.

    global class CloseCaseBatch implements Database.Batchable<sObject>{         
        global String query;
        global Database.QueryLocator start(Database.BatchableContext BC)
            query = 'Select Id,  from Case WHERE Status = \'Open\' And ' +
                                           'Status = \'Escalated\' And' +
                                           'Status = \'Breached\''; //need to define query
            System.debug('checkcase---' + query);
            return Database.getQueryLocator(query);
       global void execute(Database.BatchableContext BC, List<sObject> scope)
          List<Case> scase= (List<Case>)scope; // ? scope is case
          System.debug('checkcase---' +scope);
          Set<ID> caseIDs = new Set<ID>();
          Map<ID,List<Ticket__c>> CaseID_TicketList = new Map<ID,List<Ticket__c>>();
          for(case cI : scase)
          **// Need to setup caseID set ??**
    for (Ticket__c ticket : [Select ID, LastModifiedDate , GLOBAL_Ticket_Status__c,GLOBAL_Goal__c from Ticket__c where GLOBAL_Goal__c in :caseIDs]){
    /* List<Ticket__c> tkList = new List<Ticket__c>();
        tkList = CaseID_TicketList.get(ticket.GLOBAL_Goal__c);
    else {
    CaseID_TicketList.put(ticket.GLOBAL_Goal__c,tkList); */
            // Map case & ticket
       Boolean allClosedTickets = true;
        // **Loop thru Map on Case for all associated tickets
          //for (caseID map loop)**
          allClosedTickets = true;
          for (Ticket__c ticket: scase) { // Ticket loop
            System.debug('checkcase---' +ticket);
              **// 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**
            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'){
            if(closedTickets == totalTickets) {
                casesToUpdate.add(new Case(Id = caseID, Status= 'Closed'));
        if(!casesToUpdate.isEmpty()) {
            update casesToUpdate;
       global void finish(Database.BatchableContext BC)
           **// Finishing the Batch Apex Class**

Can anyone help me out please.


  • What's the relation to salesforce-communities ? Please try to only select tags specifically related to your questions. Do you mind removing that tag, or explaining why it should be there ? – Samuel De Rycke Feb 20 '14 at 16:54
  • Apologies for selecting the wrong tag Samuel De Rycke. Thank you for the update. – Developer Feb 20 '14 at 17:13
  • 1
    What is the state of the current functionality. Is it not accomplishing what you want? What is it doing? Are you getting any errors? – Chris Duncombe Feb 20 '14 at 17:15
  • The state of currently functionality was that i was populating all the records of cases instead of specific records. so the changed the querylocator queue set to open ,breached and escalated so thats fine. i have left comments in the code which has gaps to make it work properly. if you can help me out in filing the gaps, will be much appreciated – Developer Feb 20 '14 at 17:23
  • 1
    You have a bug in your code: if(allClosedTickets = true) should be if(allClosedTickets); the way you have it written will always evaluate to true, because you are assigning true to the value before checking the condition. == is used for comparison, = is for assignment. However, you shouldn't normally use booleanValue == true or booleanValue == false; simply say booleanValue or !booleanValue. – sfdcfox Feb 20 '14 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.