Skip to main content
Hi,

 

I'm trying to write test class for batch apex class, but its giving error System.QueryException: List has no rows for assignment to SObject.

 

Please Someone guide me test class for the given batch apex.

 

Batch Apex class:

 

global class UpdateAllContactBatch implements Database.Batchable<sObject>, Database.Stateful{

 

    global Integer recordProcessed=0;

 

    global Database.QueryLocator start(Database.BatchableContext bc){

 

       String query='select id,name,Last_modified__c,Owner.Email from Contact where Last_modified__c = LAST_N_DAYS:15';

 

       System.debug([select id,name,Last_modified__c, Owner.Email from Contact where Last_modified__c = LAST_N_DAYS:15]);

 

        return Database.getQueryLocator(query);

 

    }

 

    global void execute(Database.BatchableContext bc, List<Contact> conList){

 

            recordProcessed=recordProcessed+1;

 

    }

 

    global void finish(Database.BatchableContext bc){

 

       Contact c=[select id,name,Last_modified__c, Owner.Email from Contact where Last_modified__c = LAST_N_DAYS:15 ];

 

        System.debug(recordProcessed);

 

        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

 

        String[] toAddress=new String[]{c.Owner.Email};

 

        email.setToAddresses(toAddress);

 

        email.setSubject('Apex Batch Job completed');

 

        email.setPlainTextBody('Apex Batch '+ recordProcessed+' Records');

 

        List<Messaging.SingleEmailMessage> emails=new List<Messaging.SingleEmailMessage>{email};

 

        List<Messaging.SendEmailResult> result=Messaging.SendEmail(emails);

 

        

 

    }

 

}
4 Antworten
  1. 6. Okt. 2020, 09:51

    Hi Pallavi, 

    Change the Query in your finish method to

     

     

    Contact c=[select id,name,Last_modified__c, Owner.Email from Contact where Last_modified__c = LAST_N_DAYS:15 LIMIT 1];

     

     

     
0/9000