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
Vinothini murugesh 10Vinothini murugesh 10 

need help in writing test class for batch class

48% covered.   from here it is not covering. here the value is coming as false  if(!srList[i].isSuccess()){
}

global class categorySelectionBatch implements Database.Batchable<sObject> {
    
    public List<Account> accts;
    
    public categorySelectionBatch(List<Account> acctsToUpdate){
        accts = acctsToUpdate;
    }
    
    global Iterable<sObject> start(Database.BatchableContext BC) {
        return accts;
    }
    
    global void execute(Database.BatchableContext BC, List<sObject> sObjList) {
        //error handling
        List<Account> accountList = (List<Account>)sObjList;
        
        Database.SaveResult[] srList = Database.update(accountList, false);
        List<Error_Log__c> errorLogs = new List<Error_Log__c>();        
        for(Integer i=0; i <  srList.size(); i++){ 
            system.debug('!srList[i].isSuccess()'+srList[i].isSuccess()+'size'+srList.size());
            if(!srList[i].isSuccess()){
                
                String errorLog = String.valueof(srList[i].getErrors()[0].getStatusCode()).substring(0, math.min(String.valueof(srList[i].getErrors()[0].getStatusCode()).length(), 255)); 
                String errorDesc = String.valueof(srList[i].getErrors()[0].getMessage()).substring(0, math.min(String.valueof(srList[i].getErrors()[0].getMessage()).length(), 1000)); 
                
                errorLogs.add(new Error_Log__c(
                    Error_Log__c = errorLog,//1000
                    Error_Description__c = errorDesc,//255
                    Project__c = 'Customer_Category',
                    BatchId__c = BC.getJobId(),
                    Account_ID__c = accountList[i].Id,
                    CustomerCategoryId__c = accountList[i].Customer_Category__c
                ));
                
            }
        }
        
    }
    
    
    
    global void finish(Database.BatchableContext BC) {
        
        Integer accountUpdateFailures = 0;
        
        accountUpdateFailures = [Select count() from Error_Log__c where BatchId__c = :bc.getJobId()];
        
        AsyncApexJob a = [Select Id, Status,ExtendedStatus,NumberOfErrors, JobItemsProcessed,TotalJobItems, CreatedBy.Email from AsyncApexJob 
                          where Id =:BC.getJobId()];

        if(accountUpdateFailures > 0){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {a.CreatedBy.Email};
               String[] CcAdddresses = new String[] {'Lst-CONNECT_SUPPORT@nike.com'};
                    mail.setToAddresses(toAddresses);
            mail.setCcAddresses(CcAdddresses);
            mail.setSubject('Customer Category update failed and its batchId:' +bc.getJobId());
            mail.setPlainTextBody('Please log a case with Salesforce Connect team to process the failures. Please find more details in the Error Log object');
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });            
        }        
        
    }
    
}