2

Is it possible to run a batchable class from a List<sObject> that is passed into the constructor as opposed to generating a scope list from a query?

For example, I have a list of singleEmailMessage object, essentially composed emails ready to be sent like so: Messaging.sendEmail( email_list ); But since there is a limit of 10 emails per transaction and only so many emails within a given transaction, I need this to be batchable.

For now I have something very simple like this started but not sure where to go from here:

global class SendEmailBatch implements Database.Batchable<sObject> {
    public String query;
    public List<SingleEmailMessage> email_list;

    global SendEmailBatch(List<SingleEmailMessage> emails) {
        this.query = query;
        this.email_list = emails;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<Sobject> scope) {
        Messaging.sendEmail( email_list ); 
    }

    global void finish(Database.BatchableContext BC) {}
}

As you can see above I'm attempting to just bypass the default parameters but I'm not sure this is the best way, this is my very first batch class. My thought is I need this list in the scope variable in order for it to properly batch.

Any help you can provide to this question would be very helpful, thanks!

2
  • You should check out the custom iterators, salesforce.com/us/developer/docs/apexcode/Content/… This will help you iterate over any types of list, but when you use this in your start method of batch class you will be subject to governor limits..
    – Sam
    Commented Apr 30, 2015 at 2:05
  • 1
    You are limited to 10 sendEmail calls, but the size of your list does not have a documented limit as far as I can tell. So just put all your SingleEmailMessage instances in one list.
    – Adrian Larson
    Commented Apr 30, 2015 at 2:09

1 Answer 1

2

The below is an example of pattern that you need to follow to send bulk emails

List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
 for(integer i = 0; i < 15; i++){
   Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
   email.setSubject('email '+i+' of 15');
   email.setToAddresses( new List<String>{'[email protected]'} );
   email.setPlainTextBody('this message was generated from apex');
   emails.add(email);
 }
  Messaging.sendEmail(emails);

For each of your scope element generate a Messaging.singleEmailMesssage and outside for loop fire the Messaging.sendEmail method

Also you can use custom iteratable documented below

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_iterable.htm

2
  • Apparently I should have just test this better. I was already doing this to bulkify type method originally but I thought I would run into issues so I've been looking into the batchable class process :P Thanks so much for clarifying. Essentially then I should not have any issues no matter how many singleEmailMessage items I have, correct? Commented Apr 30, 2015 at 3:59
  • yes .You should not have any issues .whats max data volume on which this batch can run?You may hit 1000 email limit .this will be for 1000*200 records or 200K records . Commented Apr 30, 2015 at 4:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .