2

I'm fairly new to SF. I've managed to write and execute a class to update a field by batch.

What would be an appropriate way to generate and view information on how my job went for the administrator?

E.g.
Records found: 80123
records updated: 45888

4

You can use Database.Stateful to store some data between batches in variables. Consider the following example:

public class SomeBatch implements Database.Batchable<sObject>, Database.Stateful{
    public Integer processedRecords;
    public String query;
    public SomeBatch(){
        processedRecords = 0;
    }

    public Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }

    public void execute(Database.BatchableContext BC, List<sObject> scope){
        //some logic here
        //if (true){
            processedRecords++;
        //}
    }


    public void finish(Database.BatchableContext BC){
    //perform some reporting with processedRecords
    }
}

In finish method you can call some DML to insert record with summary data (processedRecords in case above) or send single email message with notification about batch result.

More info about Database.Stateful here - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm

| improve this answer | |
  • Helpful, thanks for that. I'm also confused how to present the summary data. Perhaps I should maintain a 'Process Run' object and report on submit time, processed record count, error record count etc? – Dizzley Mar 7 '16 at 11:20
  • 1
    @Dizzley yes, that may be one of the solutions. I would recommend you to make that object more general, like add extra fields about which batch is run (probably there would be more batches in future), also log some used limits data(like queries and processors time), and add that as attachment to the objects to monitor all the issues. But ensure that heap size is not overflow,cause that limit is also applied. – kurunve Mar 7 '16 at 11:30
  • yes I agree, I would begin to build a more general object which describes useful fields and activities that batch processes would have. I am on a learning curve but have general design/code/database experience. – Dizzley Mar 7 '16 at 11:41

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.