I have a simple piece of batch code that iterates over all Location__c records that don't already have Folders. It then calls a method to create the missing folders.
public with sharing class S3LinkAddBuildingFoldersBatch implements Database.Batchable<sObject>, Database.AllowsCallouts
{
public Database.QueryLocator start(Database.BatchableContext bc)
{
return Database.getQueryLocator([SELECT Id, Name
FROM Location__c
WHERE Id NOT IN (SELECT Location__c
FROM NEILON__Folder__c
WHERE Name LIKE '0%|%')]);
}
public void execute(Database.BatchableContext bc, List<Location__c> buildings)
{
set<Id> buildingIds = new set<Id>();
for(Location__c bld : buildings)
{
buildingIds.add(bld.Id);
}
boolean isAsyncJob = true;
S3LinkAddBuildingFolders.addFolders(buildingIds,isAsyncJob);
}
public void finish(Database.BatchableContext bc)
{
}
}
I have created a Test Class that creates some Location__c records, some with folders and some without. I have debugged the start query before calling the batch, and indeed, it returns one Location__c record.
DEBUG|(Location__c:{Id=a002500000FeIKEAA3, Name=006560 | 034 Somewhere Heights LE10 7PG})
When running the Test, I get the Start did not return a valid iterable object error.
I cannot see what is wrong. Can anyone help point out what I've missed?
Thanks, Kevin
UPDATE ...
Tried rewriting to simplify the iterator query. Added a constructor to get list of folders. Changed Start to just get all location__c records. Added a filter in the execute to only consider Location__c if not in folders.
However, all to no avail. Still get the same error !! I'd appreciate any insight into what is causing the error.
public with sharing class S3LinkAddBuildingFoldersBatch implements Database.Batchable<sObject>, Database.AllowsCallouts
{
set<Id> foldLocIds = new set<Id>();
public S3LinkAddBuildingFoldersBatch()
{
this.foldLocIds = foldLocIds;
for(NEILON__Folder__c fold : [SELECT Location__c FROM NEILON__Folder__c WHERE Name LIKE '0%|%'])
{
foldLocIds.add(fold.Location__c);
}
}
public Database.QueryLocator start(Database.BatchableContext bc)
{
return Database.getQueryLocator([SELECT Id, Name FROM Location__c]);
}
public void execute(Database.BatchableContext bc, List<Location__c> buildings)
{
set<Id> buildingIds = new set<Id>();
for(Location__c bld : buildings)
{
if(!foldLocIds.contains(bld.Id))
{
buildingIds.add(bld.Id);
}
}
boolean isAsyncJob = true;
S3LinkAddBuildingFolders.addFolders(buildingIds,isAsyncJob);
}
public void finish(Database.BatchableContext bc)
{
}
}