Workaround to change Batch class to use QueryLocator

In my haste to get a new batch class running I accidentally packaged it such that the start() method returns an iterable (List<SObject>) instead of a Query Locator. Bad developer, no Twinkie.

Annoyingly the interface is global so that it can be invoked via anonymous apex inside of client orgs, and yet a client’s org has too many rows in the queried object to run (> 50k). It seems that I don’t have an option to overload this: even trying to use @deprecated and adding a second start() method that leverages a Query Locator does not work.

I’ve contacted support to see if I can get the class removed from the package since this class is only in a single org anyway, but can anyone think of a good way around the problem short of adding a second class?

Answer

If you can convince the “one org” to uninstall the package, Salesforce can roll back the package to a “beta” version, which will let you fix your mistake. Your client will be inconvenienced, though, because you’ll have to export all the data, uninstall, reinstall, and reload all the data. It’s not good. Otherwise, you’ll have to go with a new class entirely. For the record, I’d personally recommend not using either of the documented return types in the future, and use Iterable<Object> instead. Iterable<Object> is implemented by List<SObject> (and all the specific types) as well as Database.QueryLocator. This gives you the flexibility to choose which you’d like to use later.


For some reason that’s beyond me, Database.QueryLocator won’t return as an Iterable<Object> unless you cast it, making the basic framework look like this:

global class BatchProcess implements 
    Database.Batchable<Object>, Database.AllowsCallouts, Database.Stateful {

    String query;

    global Iterable<Object> start(Database.BatchableContext context) {
        return (Iterable<Object>)Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext context, Object[] scope) {
    }
    global void finish(Database.BatchableContext context) {
    }
}

Attribution
Source : Link , Question Author : Matt Lacey , Answer Author : sfdcfox

Leave a Comment