0

I'm trying to iterate over a Set that is sent to my Batch Class, I am getting an error stating the following:

Class FileCleanup must implement the method: void Database.Batchable.execute(Database.BatchableContext, List) (Line: 1, Column: 14)

Can someone explain what I'm doing wrong and how I should correct iterate over the set?

Here is my code

global class FileCleanup implements Database.Batchable<String>, Database.AllowsCallouts {

    //String query;
    public Set<String> cleanupList;

    global FileCleanup(Set<String> cleanupList) {
        this.cleanupList = cleanupList;
    }

    global Iterable<String> start(Database.BatchableContext BC) {
        return cleanupList;
    }

    global void execute(Database.BatchableContext BC, Set<String> scope) {

        /// do some logic here
    }

    global void finish(Database.BatchableContext BC) {

    }

}

2 Answers 2

3

Set doesn't implement Iterable, but it behaves like an Iterable if you coerce it. Note, however, that the execute method will always only accept a List as the second parameter, because the set is implicitly converted to a list as part of the batchable serialization.

global class FileCleanup implements Database.Batchable<String>, Database.AllowsCallouts {
    global Set<String> cleanupList;

    global FileCleanup(Set<String> cleanupList) {
        this.cleanupList = cleanupList;
    }

    global Iterable<String> start(Database.BatchableContext BC) {
        // Cast here to force set to behave correctly
        return (Iterable<String>)cleanupList;
    }

    global void execute(Database.BatchableContext BC, String[] scope) {
        // Scope is now a list, but it will be unique values only
        /// do some logic here
    }

    global void finish(Database.BatchableContext BC) {

    }
}
1

A simple way to go is is this:

global Iterable<String> start(Database.BatchableContext BC) {
    return new List<String>(cleanupList);
}

as new List<String> automatically implements Iterable<String>. That is not true for new Set<String> - see the unimplemented idea Apex Code: Implement Iterable on Set.

(But I can't explain why the error message you get is so obscure.)

3
  • Actually... It's more complicated than that idea lets on...
    – sfdcfox
    Aug 15, 2018 at 23:46
  • Oh, and FYI, the execute method was actually incorrect, hence the compiler error.
    – sfdcfox
    Aug 15, 2018 at 23:51
  • @sfdcfox Ah makes sense - I didn't notice that.
    – Keith C
    Aug 15, 2018 at 23:52

You must log in to answer this question.

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