• sai kumar 433
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 10
    Questions
  • 4
    Replies
please execute this test class for code coverage.........
global class  BatchClass5 implements  Database.Batchable<sObject> {
   global final string query;
  global  BatchClass5(String q)
      {
         query = q;
      }
      
      global Database.QueryLocator start(Database.BatchableContext BC){
      
         return Database.getQueryLocator(query);
      }
      
      global void execute(Database.BatchableContext BC, List<Account> scope)
      {
          List<Account> accns = new List<Account>();
       List<Contact> lstcon= new List<contact>();
         for(Account a : scope)
         {
                 Account a1 = new Account();
                 contact c = new contact();
                 c.lastname = a1.name;
                 c.accountid = a1.id; 
                  lstcon.add(c);
                  }
          
      insert lstcon;
      }
      global void finish(Database.BatchableContext BC){
        // Get the ID of the AsyncApexJob representing this batch job  
        // from Database.BatchableContext.    
        // Query the AsyncApexJob object to retrieve the current job's information.  
       
      }
}

testclass

@isTest
private class BatchClass5_Test5 
{
    static testmethod void m1()
    {
        
        Test.startTest();
        Account a = new Account(Name='saikumar');
            insert a;
        contact c = new Contact();
        c.LastName =a.Name;
        c.AccountId= a.Id;
        insert c;
                system.assertEquals('saikumar',c.LastName);

        BatchClass5 sync = new batchclass5('select id,name from Account');
        ID batchprocessid = Database.executeBatch(sync);
        Test.stopTest();
    }

}
global class BatchClass2 implements Database.Batchable<sObject>{
   global final String Query;
   global final String Entity;
   global final String Field;
   global final String Value;

   global BatchClass2(String q, String e, String f, String v){
             Query=q; Entity=e; Field=f;Value=v;
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, 
                       List<sObject> scope){
      for(Sobject s : scope){s.put(Field,Value); 
      }      update scope;
   }

   global void finish(Database.BatchableContext BC){

   }

}

test class


@isTest
public class BatchClass_Test2 
{
    static testMethod void test() {
        Database.QueryLocator QL;
        Database.BatchableContext BC;
        List<Account> AcctList = new List<Account>();
        BatchClass2 AU = new BatchClass2('Name', 'Test');
        QL = AU.start(bc);
        
        Database.QueryLocatorIterator QIT =  QL.iterator();
        while (QIT.hasNext())
        {
            Account Acc = (Account)QIT.next();            
            System.debug(Acc);
            AcctList.add(Acc);
        }        
        
        AU.execute(BC, AcctList);
        AU.finish(BC);        
    }
}
I am executing the batch manually from the developer console and I am getting this error. 

System.LimitException: Too many query rows: 50001

Details Below: 

Batch Execution:
batchDataGeneration batchable = new batchDataGeneration();
database.executeBatch(batchable);

Error Information: 
14:09:41:000 FATAL_ERROR Class.Database.QueryLocatorIterator.hasNext: line 41, column 1
14:09:41:000 FATAL_ERROR Class.batchDataGeneration.execute: line 516, column 1

This is the code from Line 41 and Line 516.

Line 41 Code:

return Database.getQueryLocator([SELECT id,
            Name,
            Frequency__c,
            Start_Date__c,
            Activated_Date__c, //Line 41
            Date_to_Create_Next_Year_s_Targets__c
            FROM Measure__c
            where
            status__c =:System.Label.Activated
        ]);

Line 516 code:

Database.QueryLocator qplant = Database.getQueryLocator([Select Id, Fiscal_Period__c, Plant__c,Date__c, Measure__c from Plant_Measure__c where Measure__c in : measureToExecute]);
            Database.QueryLocatorIterator plantIterator =  qplant.iterator();
            while (plantIterator.hasNext()) //Line 516
            {
                    Plant_Measure__c a = (Plant_Measure__c)plantIterator.next();
                    mapPlantMeasureIds.put(a.id,a);
            }

I know the counts are based on the transaction and not on a single query output. I have used Database.querylocator and still I am getting this error. Could someone help me what could be the problem with this code?
global class UpdateContactAddresses implements 
    Database.Batchable<sObject>, Database.Stateful {
    
    // instance member to retain state across transactions
    global Integer recordsProcessed = 0;

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'SELECT ID, BillingStreet, BillingCity, BillingState, ' +
            'BillingPostalCode, (SELECT ID, MailingStreet, MailingCity, ' +
            'MailingState, MailingPostalCode FROM Contacts) FROM Account ' + 
            'Where BillingCountry = \'USA\''
        );
    }

    global void execute(Database.BatchableContext bc, List<Account> scope){
        // process each batch of records
        List<Contact> contacts = new List<Contact>();
        for (Account account : scope) {
            for (Contact contact : account.contacts) {
                contact.MailingStreet = account.BillingStreet;
                contact.MailingCity = account.BillingCity;
                contact.MailingState = account.BillingState;
                contact.MailingPostalCode = account.BillingPostalCode;
                // add contact to list to be updated
                contacts.add(contact);
                // increment the instance member counter
                recordsProcessed = recordsProcessed + 1;
            }
        }
        update contacts;
    }    

    global void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + ' records processed. Shazam!');
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, 
            JobItemsProcessed,
            TotalJobItems, CreatedBy.Email
            FROM AsyncApexJob
            WHERE Id = :bc.getJobId()];
        // call some utility to send email
            }    

}