0

my batch class is given below:-

public without sharing class BatchFirstResponse implements Database.Batchable<sObject> {
    public static void run( Set<Id> CaseIds ) {

        List<Case> CaseRecords =  [Select id, First_Response__c, 
                                   ClosedDate, 
                                   OwnerId, CreatedDate,Status, 
                                   Helper_Closed_Date__c,
                                   LastModifiedDate,
                                   Helper_First_Responce__c
                                   from case 
                                   where Id IN: CaseIds ];
        executeHelper( CaseRecords );                  
    }

    public Database.QueryLocator start(Database.BatchableContext BC){
        string query = 'Select id, First_Response__c,'+ 
            'ClosedDate ,'+ 
            'OwnerId, CreatedDate, Status,'+ 
            'Helper_Closed_Date__c, '+
            'LastModifiedDate, '+
            'Helper_First_Responce__c '+
            'from case'  ; 

        System.debug('query'+query);
        return Database.getQueryLocator(query);
    }
    public void execute(Database.BatchableContext BC, List<Case> cases){

        executeHelper( Cases );
    }

    public void finish(Database.BatchableContext BC){

    }
    public static void executeHelper( List<Case> CaseRecords ){
        List<Case> caseList = new List<Case>();
        Set<Id> caseIds = new Set<Id>();
        BusinessHours defaultBH = [ SELECT Id 
                                   FROM BusinessHours 
                                   WHERE IsDefault = true  
                                   Limit 1
                                  ];

        for(Case caseObj : CaseRecords){
            caseIds.add(caseObj.Id);
        }
        Map<Id, DateTime> lastModDateTimeMap = new Map<Id, DateTime>();
        lastModDateTimeMap = firstUpdation(CaseRecords , caseIds);
        for(Case caseObj : CaseRecords ){ 
            if (caseObj.First_Response__c == Null)
               { 

                   if(lastModDateTimeMap.get(caseObj.Id) != null){

                       Decimal result = BusinessHours.diff( defaultBH.Id, caseObj.CreatedDate, lastModDateTimeMap.get(caseObj.Id) );

                       Decimal resultingHours = result/(60*60*1000);

                       caseObj.Helper_First_Responce__c= resultingHours;

                       Decimal HH=resultingHours.round(System.RoundingMode.DOWN);

                       Decimal tempMM = ( resultingHours - resultingHours.round(System.RoundingMode.DOWN))  * 60;

                       Decimal MM = tempMM.round(System.roundingMode.DOWN);

                       Decimal SS = (( tempMM - tempMM.round(System.RoundingMode.DOWN)) * 60).round(System.roundingMode.DOWN);


                       if( ( HH != 0.00 || MM != 0.00 || SS != 0.00 )  ){
                           string finaltime = ''+HH+':'+MM+':'+SS;
                           caseObj.First_Response__c = finaltime;
                           caseObj.Helper_First_Responce__c = resultingHours; 
                       }

                   }
               }

            caseList.add(caseObj);
        }

        update caseList;
    } 

    public static Map<Id, DateTime> firstUpdation (List<Case> CaseRecords, Set<Id> caseIds){

        Map<Id, CaseHistory> mapCaseHistory = new Map<Id, CaseHistory>();
        Map<Id, Event> mapOfEvents = new Map<Id, Event>();
        Map<Id, Task> mapOfTasks = new Map<Id, Task>();
        Map<Id, DateTime> finalCaseMap = new Map<Id, DateTime>();

        for(CaseHistory ch : [SELECT CaseId,CreatedById,
                              CreatedDate,Field,Id, 
                              IsDeleted, NewValue,OldValue 
                              FROM CaseHistory  
                              where caseId IN: caseIds
                              AND Field != 'created'
                              ORDER BY createdDate ASC
                              LIMIT 1]){
                                  mapCaseHistory.put(ch.caseId , ch);     
                              }


        for(Event eveObj : [SELECT id, whatId, 
                            createdById, createdDate 
                            FROM Event  
                            where whatId =: caseIds
                            ORDER BY createdDate ASC
                            LIMIT 1]){
                                mapOfEvents.put(eveObj.whatId, eveObj);
                            }

        for(Task taskObj : [SELECT id, whatId, 
                            createdById, createdDate 
                            FROM Task  
                            where whatId =: caseIds
                            ORDER BY createdDate ASC
                            LIMIT 1]){
                                mapOfTasks.put(taskObj.whatId, taskObj);    
                            }

        for(Case caseObj: CaseRecords){
            List<DateTime> dateTimes = new List<DateTime>();
            if(mapCaseHistory.size() > 0){
                if(mapCaseHistory.get(caseObj.Id).createdById == caseObj.OwnerId){
                    dateTimes.add(mapCaseHistory.get(caseObj.Id).createdDate);    
                }

            }
            if(mapOfEvents.size() > 0){
                if(mapOfEvents.get(caseObj.Id).createdById == caseObj.OwnerId){
                    dateTimes.add(mapOfEvents.get(caseObj.Id).createdDate); 
                }

            }
            if(mapOfTasks.size() > 0){
                if(mapOfTasks.get(caseObj.Id).createdById == caseObj.OwnerId){
                    dateTimes.add(mapOfTasks.get(caseObj.Id).createdDate); 
                }
            }

            dateTimes.sort();
            if(dateTimes.size() > 0 ){
                finalCaseMap.put(caseObj.Id, dateTimes.get(0)); 
            }
        }
        return  finalCaseMap; 
    }

} 

how to cover below part:- enter image description here


Any suggestions? my test class is given below:-

@isTest
private without sharing  class BatchFirstResponseTest {
static testmethod void BatchClosedResponseMethod(){

        Case caseObj1 = new Case();
        caseObj1.Status ='Working';
        caseObj1.Origin ='Email';
        caseObj1.Helper_Closed_Date__c= 9;
        caseObj1.Helper_First_Responce__c= 12;
        caseObj1.First_Response__c = 'me';
        caseObj1.OwnerId = UserInfo.getUserId();
         insert caseObj1;
    Account a= new Account ();
    a.name='Test Account';
    insert a;

    Contact cont = new Contact(lastname = 'Test Contact');
    insert cont;
    Case cse = new Case(Status = 'new',
                        Origin= 'Email',
                        Reason = 'Tax Filling', 
                        First_Response__c= Null);
    insert cse;
    cse.OwnerId = UserInfo.getUserId();
    update cse;  

    Task t = new Task();
    t.WhatId=cse.id;
    insert t;
      Event e = new Event();
        e.WhatId=cse.id;
        e.StartDateTime=system.today();
        e.EndDateTime=system.today()+5;
        insert e;

        Test.startTest();

        caseObj1.Status ='Closed';
        caseObj1.OwnerId = UserInfo.getUserId();

        update caseObj1;
        BatchFirstResponse obj = new BatchFirstResponse();
        //obj.execute(NULL,lstCase);
        DataBase.executeBatch(obj);
        Test.stopTest();
}
     static testmethod void BatchFirstResponseMethod01(){

        Case caseObj = new Case(); 
        caseObj.Status ='Working';
        caseObj.Origin ='Email';
        caseObj.Helper_Closed_Date__c= 9;
        caseObj.Helper_First_Responce__c= 12;
        insert caseObj;
        Set<Id> caseIds = new Set<Id>();
        caseIds.add(caseObj.Id);
        Test.startTest();
        BatchFirstResponse.run(CaseIds);

        Test.stopTest();
    }
}
}

Any suggestions? how to cover the part of the batch?

1
  • 1
    Please include the relevant lines of your test class too. See How to Ask for how to improve your question which will help you get a useful answer.
    – Dave Humm
    Jul 20, 2018 at 8:07

1 Answer 1

1

Have you created Task and Event records in your test method? Because you can't see CaseHistory records in the test method. If you want to see the History records then you will have to use the SeeAllData=true in your test methods.

You are not passing any case id in the BatchFirstResponse constructor in BatchClosedResponseMethod but you are doing it in the another method but there is no task or event created for the case.

static testmethod void BatchClosedResponseMethod(){

    Case caseObj1 = new Case();
    caseObj1.Status ='Working';
    caseObj1.Origin ='Email';
    caseObj1.Helper_Closed_Date__c= 9;
    caseObj1.Helper_First_Responce__c= 12;
    caseObj1.First_Response__c = 'me';
    caseObj1.OwnerId = UserInfo.getUserId();
    insert caseObj1;
    Account a= new Account ();
    a.name='Test Account';
    insert a;

    Contact cont = new Contact(lastname = 'Test Contact');
    insert cont;
    Case cse = new Case(Status = 'new',
                    Origin= 'Email',
                    Reason = 'Tax Filling', 
                    First_Response__c= Null);
    insert cse;
    cse.OwnerId = UserInfo.getUserId();
    update cse;  

    Task t = new Task();
    t.WhatId=cse.id;
    insert t;
    Event e = new Event();
    e.WhatId=cse.id;
    e.StartDateTime=system.today();
    e.EndDateTime=system.today()+5;
    insert e;

    Test.startTest();

    caseObj1.Status ='Closed';
    caseObj1.OwnerId = UserInfo.getUserId();

    update caseObj1;
    BatchFirstResponse obj = new BatchFirstResponse(});
    BatchFirstResponse.run(new Set<Id> {caseObj1.Id)
    //obj.execute(NULL,lstCase);
    DataBase.executeBatch(obj);
    Test.stopTest();
}
1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Adrian Larson
    Jul 20, 2018 at 17:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .