I have a batch job which gets a number of users in the start interface.

So I implement the following APIs

global Database.QueryLocator start(Database.BatchableContext bc) 
global void execute(Database.BatchableContext bc, List<User> users) 
global void finish(Database.BatchableContext bc) 

I am wondering is there anyway I can get a handle to all users (not a batch of users) in the finish method?

Note: I was hoping to do this and keep my class stateless.



You would need to implement the Stateful interface and for every iteration of your execute method, you would need to add the users to a member level users list, e.g.

allUsers.addAll( users );

And then reference this in your finish method.

However, be wary of the maximum heap size (15mb I think), although, I guess you'd have to be querying a lot of users with a lot of data to exceed it.

If you don't want to maintain state using the Stateful interface, you would have to store the users in a custom object. You could serialise them to a text file attachment or some other method of self rolled state.

  • Hawthon. I try to avoid state everywhere. But thanks for suggestions. I will update question to be clearer. – dublintech Jan 10 '13 at 15:54
  • 1
    @dublintech, ok i edited with another suggestion if you don't want to use Stateful – Phil Hawthorn Jan 10 '13 at 15:57

If you want to keep it stateless still you can query all the Users in finish method provided the context limits dont get hit like 50,000 query rows limit .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.