Batch Class Error – System.UnexpectedException: Start did not return a valid iterable object

batchgetrest-api

I m trying to Sync Salesforce with an external system and run the batch class every night but when I try to test the batch class in Execute anonymous window, I get this – System.UnexpectedException: Start did not return a valid iterable object. Below is the code.

global class objSync {

public static list<custom obj> upsertRecs(){
   
   
    List<custom obj> recList = new List<custom obj>();   
    integer page = 1;
    integer Size = 100;
    Integer responseSize;  
    
    do{
        string url = 'https://api.etlHub.com/&page='+page+'&from_date=09-02-2021';
        request.setEndpoint(url);
        
          request.setMethod('GET'); 
        request.setHeader('Content-Type','application/json');
        request.setHeader('Ocp-Apim-Subscription-Key','xxxx');
        request.setTimeout(120000);
        System.debug('** Request is: ' + request); 
        
        HttpResponse response = http.send(request);
        system.debug('response'+response);
        system.debug('response body'+response.getbody()); //get the external system data
        String responseBody = response.getBody();
        GetResponseBodyClass ParsedResponse = new GetResponseBodyClass();
        system.debug('parsed response before'+ParsedResponse);
        ParsedResponse = (GetResponseBodyClass)JSON.deserialize(responseBody, GetResponseBodyClass.class);       
        system.debug('parsed response after'+ParsedResponse);
        
        Map<String,results> targetResults = new Map<String,results>();            
       
        system.debug('results size'+ParsedResponse.results.size());
        responseSize = ParsedResponse.results.size();
        
       
        for(Results re : ParsedResponse.results){          
            targetResults.put(re.uniqueKey, re);               
        }
        
        String accessMeds='';
        String bldgCategories='';
        if(targetResults.keySet().size()>0){   
            system.debug('inside target results');
            for(Results re : ParsedResponse.results){
                Custom obj co = new Custom Obj(Building_Name__c=re.buildingname, 
                                                                         Street_Name__c = re.streetName,
                                                                         Street_Number__c = re.primaryNumber,
                                                                        
                                                                         country__c = re.country,
                                                                                                                                              
                                                                         city__c = re.city, 
                                                                         Zip_Code__c = re.postal,
                                                                                                                                             
                                                                         external_id__c = re.uniqueKey,
                                                                         
                                                                        );
                
                recList.add(co);        
                
            } 
        }
        page++;
        system.debug('page value after'+page);
    } while(responseSize == size);
    upsert recList external_id__c;
    return null;
} 

}

Here is the batch class –

global class SyncWithBatchCls implements Database.Batchable, Database.stateful, Database.AllowsCallouts{

global List<Custom Obj> tlList = new list<Custom Obj>();



global SyncWithBatchCls(){   
    this.tlList = objSync.upsertRecs();
}

global List<Custo Obj> start(Database.BatchableContext bc){              
    return tlList;
}

global void execute(Database.BatchableContext bc, List<Custom Obj> tl){        
    Database.insert(tl);        
}
global void finish(Database.BatchableContext bc){ 
}

}

Best Answer

In your upsertRecs method you are returning null. You should return recList.