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
naveen reddy 19naveen reddy 19 

Batch apex for list of ids : First error: Start did not return a valid iterable object.

Hi,

 I have list of ids and these Ids should be processed using batch apex.

I'm getting below error while executing batch apex.

First error: Start did not return a valid iterable object.
 
global class BatchClass implements Database.Batchable<Id> {
    global string query;
    List<Id> ids;
    String type;

    global BatchClass(List<Id> ids,String type) {
        ids= new List<Id>();
        ids=this.ids;
        type=this.type;
        System.debug('########## constructor'+ids);
    }

    global Iterable<Id> start(Database.BatchableContext BC){
        System.debug('###########'+ids);
        return ids;
    }

    global void execute(Database.BatchableContext BC, List<Id> idsToProcess) {
       
	   //Processing
       // 
    }
    
    global void finish(Database.batchableContext bc){ 
    }

}
 

BatchClass b= new BatchClass(Ids,'Process');//Ids : List of Ids
Database.executeBatch(b);

**Any help is really appriciated. Thanks in advacne.

Thanks,
Naveen.

 
Best Answer chosen by naveen reddy 19
Shashikant SharmaShashikant Sharma
Hi Naveen,

Please change you Constructor as below, here left hand side is having this.Ids rather then right hand side like you used.
 
global BatchClass(List<Id> ids,String type) {
        
        this.ids= ids;
        this.type=type;
        System.debug('########## constructor'+ids);
    }

It will work fine. 

Thanks
Shashikant

All Answers

Shashikant SharmaShashikant Sharma
Hi Naveen,

Please change you Constructor as below, here left hand side is having this.Ids rather then right hand side like you used.
 
global BatchClass(List<Id> ids,String type) {
        
        this.ids= ids;
        this.type=type;
        System.debug('########## constructor'+ids);
    }

It will work fine. 

Thanks
Shashikant
This was selected as the best answer
naveen reddy 19naveen reddy 19
Hi Shashikant,

 Thanks for your valuable  response. Now that exception is avoided. But below is the output I'm getting.
 
global class BatchClass implements Database.Batchable<Id> {
    global string query;
    List<Id> ids;
    String type;

    global BatchClass(List<Id> ids,String type) {
        ids= new List<Id>();
        this.ids=ids;
        this.type=type;
        System.debug('########## constructor'+ids);
    }

    global Iterable<Id> start(Database.BatchableContext BC){
        System.debug('###########'+ids);
        return ids;
    }

    global void execute(Database.BatchableContext BC, List<Id> idsToProcess) {
       
	   //Processing
       // 
    }
    
    global void finish(Database.batchableContext bc){ 
    }

}


 
System.debug('#######start batch'+ids);
BatchClass b= new BatchClass(Ids,'Process');//Ids : List of Ids
Database.executeBatch(b);

output is:
 
02:21:08.0 (19581413)|USER_DEBUG|[43]|DEBUG|#######start batch(ka8j0000000Kz7nAAC)
02:21:08.0 (21616222)|USER_DEBUG|[21]|DEBUG|########## constructor()
02:21:08.0 (15472284)|USER_DEBUG|[25]|DEBUG|###########()

Can you advice me how to pass these Ids to execute() method.

Thanks,
Naveen.

 
Shashikant SharmaShashikant Sharma
You dont requre this statement in you Constructor : 
ids= new List<Id>(); 
See the constructor that I mentioned in my first resposne. It did not had that.
Shashikant SharmaShashikant Sharma
Hi Naveen,

Were you able to resolve the issue with above change ?

Thanks
Shashikant
naveen reddy 19naveen reddy 19
Yes Shahsikanth.

Had some other issue but able to resolve that. Made below changes.
 
global List<Id> ids;
 global String type;



You answer also helped me in resolving above exception I'm marking it as best answer.

Thanks,
Naveen.
Shashikant SharmaShashikant Sharma
Hi Naveen,

Good that my answer helped, I think you marked the other post which swas during discussion was going on. I think main issue was in was wrong placing of this in the constructor as without maing it global batch was running.  Just make sure mark the appropriate post so that others could be benifitted from this discussion.

Thanks
Shashikant