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;  
        string url = 'https://api.etlHub.com/&page='+page+'&from_date=09-02-2021';
        System.debug('** Request is: ' + request); 
        HttpResponse response = http.send(request);
        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='';
            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,
        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){        
global void finish(Database.BatchableContext bc){ 


1 Answer 1


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

  • I tried that - return recList. But its throwing a different error now - INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
    – Kushi tin
    Commented Sep 7, 2021 at 15:35
  • That’s another issue. You are doing upsert (so Id is generated) and then you try to insert again records. Do your logic and an update/upsert in the execute method. Commented Sep 7, 2021 at 15:56
  • Thank you. That worked.
    – Kushi tin
    Commented Sep 7, 2021 at 19:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .