+ Start a Discussion
Alejandro RosanoAlejandro Rosano 

Return a List from a Database.Batchable class

global class ClassName implements Database.Batchable<sObject> {
    global final string query;
    
    global UpdateUserBloombergFields (string q) {
             query=q;
    }
    
    global Database.QueryLocator start (Database.BatchableContext BC) {
        return Database.getQueryLocator(query);
    }
    
    global void execute (Database.BatchableContext BC, List<User> scope) {
        List<User> UserList = new List<User>();
        for (User u: scope) {
            UserList.add(u);
        }
    }
    
    global void finish (Database.BatchableContext BC){}
}
I would like to return the list UserList so I can use it after in any other class. Is it possible?

Thanks and kind regards.
ManojjenaManojjena
Hey Alejandra ,
  In this case execute method's return type is void .So you can not return any thing from this .
Again If you want to create a  class level varibale and assign to that you have to add Datebase.stateful in cass level which will help you to make the varibale pesistant up to last execution of the batch ,means if your execute method will execute 4 times then alll user list will add to the user list .
If you have any doubt please let me know .