You need to sign in to do that
Don't have an account?
How to Increase Code Coverage for Batch Apex Delete?
Hello all,
My code is working, however I cannot deploy because my Code Coverage is stuck at 66%. Can someone please take a look?
Thank you.
Robert
Batch_DeleteQuote:
/*
* Description : This is Batch job to perform Delete operation on Quote Records.
*
* Quotes are Deletable when Quote is 42 days or older.
* Formula "TODAY() > Date__c + 42" sets Deletable__c = TRUE
*
* Created By : Robert Wambold
*
* Created Date : 08/09/2018
*
* Version : v1.0 - Create
*/
//Batch Class
global class Batch_QuoteDelete implements Database.Batchable<sObject>,Database.stateful {
//Variable to count Success and Error Records
public Integer successCounter = 0;
public Integer failureCounter = 0;
//Start Method
global Database.QueryLocator start(Database.BatchableContext BC) {
//Query to Fetch Records
return Database.getQueryLocator([SELECT ID FROM Quote__c WHERE Deletable__c = TRUE]);
}
//Execute Method
global void execute(Database.BatchableContext BC, List<Quote__C> scope) {
//Delete the Records those are in Contexts
Database.DeleteResult[] delresults = Database.delete((scope),false);
//Loop through records going to be deleted
for(Database.DeleteResult dr : delResults){
//If record is not successfully deleted
if(!dr.isSuccess()){
//List to hold Error
Database.Error[] errs = dr.getErrors();
//Loop through list of Error
for(Database.Error err : errs) {
//Debug to get Error Status
System.debug('$#$#$#$' + err.getStatusCode() + ' - ' + err.getMessage());
//Debug to get Error Message
System.debug('ErrorMessage##########'+ err.getMessage());
}
//Increase counter for Failure
failureCounter++;
}
else {
successCounter++;
}
}
}
//Finish Method
global void finish(Database.BatchableContext BC){
// Query the AsyncApexJob object to retrieve the current job's information.
AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob WHERE Id = :BC.getJobId()];
// Send an email to the Apex job's submitter notifying of job completion.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Quote Delete Job ' + a.Status);
mail.setPlainTextBody
('Apex Quote Delete Job has been completed. There were total of ' + a.TotalJobItems
+ ' batches with ' + successCounter + ' success and ' + failureCounter
+ ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
}
}
Test Class:
@IsTest
public class Batch_QuoteDeleteTEST
{
static testMethod void testMethod1()
{
Test.startTest();
Batch_QuoteDelete obj = new Batch_QuoteDelete();
DataBase.executeBatch(obj);
Test.stopTest();
}
static testMethod void testMethod2()
{
Test.startTest();
Batch_QuoteDelete obj = new Batch_QuoteDelete();
DataBase.executeBatch(obj);
Test.stopTest();
}
}