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
Amit Vyas 8Amit Vyas 8 

Code coverage is 100% is sandbox but when deploying to production it gives Code coverage error saying the code coverage is 71%

Can anyone help me with the Test class for the batch class(to delete records which are 180 days old) I have written, I'm getting 100% code coverage in sandbox but when I'm deploying the code in production and validating using "run specified test - giving the name of my test class"  I'm getting Error as "Your Code coverage is 71%, you need at least 75% coverage to complete this deployment" 

Here is my batch class and test class:

global class DeleteOpportunityProductSnapshotBatch implements Database.Batchable<sObject>, Schedulable    
    global Database.QueryLocator start(Database.BatchableContext BC)
        return Database.getQueryLocator([SELECT Id FROM OpportunityLineItem_Snapshot__c WHERE CreatedDate != LAST_N_Days:180]);
    global void execute(Database.BatchableContext BC, List<SObject> records)
    delete records;
    global void finish(Database.BatchableContext BC) { }
    global void execute(SchedulableContext ctx) 

Test Class:
public class DeleteOppProductSnapshotBatch_Test 
    public static testMethod void testMethod1()
        OpportunityLineItem_Snapshot__c Ols = new OpportunityLineItem_Snapshot__c();
        Ols.Name = 'Test';
        Ols.CreatedDate =;
        insert Ols;
        DeleteOpportunityProductSnapshotBatch Dop = new DeleteOpportunityProductSnapshotBatch();
        id batchprocessid = Database.executeBatch(Dop);
        DeleteOpportunityProductSnapshotBatch sh1 = new DeleteOpportunityProductSnapshotBatch();
        String sch = '0 0 2 * * ?';
        system.schedule('Test Territory Check', sch, sh1);

        System.assert(batchprocessid != null);
SwethaSwetha (Salesforce Developers) 
HI Amit,
Based on your code, I see that you are not using SeeAllData=True which might reduce the code coverage as it queries data from specific org.

Are you running tests in the sandbox & deploying with different users/profiles? If so, missing some permission for the deployment user might reduce the coverage.


Similar scenarios:

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you