Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

Thanks.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
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 .

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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