I am getting an error like "Argument 1 cannot be null" while running this batch apex Batchupdate_Montly_DepthTracker. Please note this batch runs on result sets returned from a scheduled apex.

Assumption: Is it because I am running the batch apex class without running the scheduled apex and the result sets are not returned?

Please find class- Batchupdate_Montly_DepthTracker

global class Batchupdate_Montly_DepthTracker implements Database.Batchable<sObject> {
   public String query;
   // Record types for Deapth Tracker
   private String readonlyRecordtype = 'OAPI_HOS_Depth Tracker_Realonly';
   private String FirstdayRecordtype = 'OAPI_HOS_Depth Tracker_FirstDay';
   private String DefaultRecordtype = 'FutureMonth';

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

   global void execute(Database.BatchableContext BC, List<sObject> scope){

    list<Depth_Tracker_OAPI__c> dts = new list<Depth_Tracker_OAPI__c>();
    Map<String,Id> recordtypeforEndoftheMonth = new  Map<String,Id>();
    //Getting the Record types for Depth Tracker object
    for(RecordType rect: [SELECT Id,Name FROM RecordType WHERE SobjectType = 'Depth_Tracker_OAPI__c']){
        recordtypeforEndoftheMonth.put(rect.name.touppercase(), rect.Id);

     Date todaydate = Date.TODAY();
     for(sObject s : scope){Depth_Tracker_OAPI__c dt = (Depth_Tracker_OAPI__c)s;

        //For past Depth Trackers
         if( dt.End_Date_OAPI__c.isSameDay(todaydate) || dt.End_Date_OAPI__c < todaydate )  {
            dt.RecordTypeId = recordtypeforEndoftheMonth.get(readonlyRecordtype.toUpperCase());
            dt.Active_OAPI__c = false; 
          //For Depth Trackers for current month
         else if(dt.Start_Date_OAPI__c.Month() == todaydate.month() && dt.Start_Date_OAPI__c.year() == todaydate.year() ){
            dt.RecordTypeId = recordtypeforEndoftheMonth.get(FirstdayRecordtype.toUpperCase());
            dt.Active_OAPI__c = true;
         //Future Depth Trackers
            dt.RecordTypeId = recordtypeforEndoftheMonth.get(DefaultRecordtype.toUppercase());
            dt.Active_OAPI__c = true;


                    //update Contcts;
                    list<Database.Saveresult> sr = database.Update(dts);
                    system.debug('Saveresult' + sr);
                    Set<Id> scRows;
                    if(sr != null) {
                        scRows = new Set<Id>();
                        for(Database.SaveResult r : sr) {
                            if(r.isSuccess() == false) {
                        system.debug('Update failed for Depth Tracker: '+ scRows );
            catch(DMLException ex){
                System.debug(LoggingLevel.ERROR,'Error while trying to updating Depth Trackers. ' +ex.getStackTraceString());
      //Update dts;

   global void finish(Database.BatchableContext BC){

The scheduled batch code is as below

global class scheduledBatchableForDepthTrackerLock implements Schedulable {
    global void execute(SchedulableContext sc) {
        Batchupdate_Montly_DepthTracker dtBatch = new Batchupdate_Montly_DepthTracker(); 
        dtBatch.query ='SELECT Id, End_Date_OAPI__c, Start_Date_OAPI__c, RecordTypeId FROM Depth_Tracker_OAPI__c';

        Batchupdate_Montly_HCPDepthTracker HCPdtBatch = new Batchupdate_Montly_HCPDepthTracker();
        HCPdtBatch.query = 'SELECT Id, End_Date_OAPI__c, Start_Date_OAPI__c, RecordTypeId FROM HCP_Depth_Tracker_OAPI__c';
  • 1
    What is the line of Apex that is giving you that error? – Daniel Ballinger Jul 28 '14 at 8:14
  • return Database.getQueryLocator(query); This is the line to return the result set @Daniel – Testing_SFDC Jul 28 '14 at 8:56

In you start method of the class the implements Database.Batchable you will need to return a database.querylocator using Database.getQueryLocator(query);

The error is indicating that the SOQL string query is null. Try setting this to a SOQL query that returns the records you want the batch to execute on.

global class Batchupdate_Montly_DepthTracker implements Database.Batchable<sObject> {
   // You need to set this Member to a SOQL query.
   global String query;

   global Batchupdate_Montly_DepthTracker() {
       // Add the other fields you need to query and the where clause, etc...
       this.query = 'SELECT Id, End_Date_OAPI__c, Start_Date_OAPI__c, RecordTypeId FROM Depth_Tracker_OAPI__c';

   global Database.QueryLocator start(Database.BatchableContext BC) {
      System.assertNotEquals(null, query, 'The query should be defined');
      return Database.getQueryLocator(query);

   global void execute(Database.BatchableContext BC, List<sObject> scope) {

    // ...
  • That query results are returning from a schedulable batch apex. Can you help me how to deal with it? – Testing_SFDC Jul 28 '14 at 9:14
  • @saikat Do you want to use an iterable collection rather than a SOQL query to drive the batch? – Daniel Ballinger Jul 28 '14 at 9:19
  • I want to pass soql result sets from the schedulable apex batch to it's sub ordinate batch and perform update operations needed. I was running the subordinate batch from developer console to perform the above activity since schedulable batch is not run from developer console. Please let me know how to proceed here. @Daniel Please find the schedulable batch I have editted in the question section. – Testing_SFDC Jul 28 '14 at 9:21
  • @saikat Either move the query into the constructor (it could be a parameter if required) and assert that is has a value. Or switch to returning Iterable from the start method and returning a List of sObjects. – Daniel Ballinger Jul 28 '14 at 9:33
  • Did the first option.Running successfully. Thanks Daniel!! – Testing_SFDC Jul 29 '14 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.