5

Creating a batch job to load test rest callout but getting the error "batchable instance is too big" Here is my code:

public void TestData_CM(integer a){
        //this list has 10 possible outcomes of case
        List<String> types = type.split(',');

//postBody is a method which grabs all fields on case in json string format. There are some 100000 cases and I need to create call out for all these cases

            List<String> bodyList1 = new List<String>();
            for(integer i = 0; i< a; i++){
                for(String s : types){
                    String str1 = postBody('ServiceOpen', s, 'Open');
                    bodyList1.add(str1);                       
                }
            }
            Database.executeBatch(new caseTriggerBatch(bodyList1));

    }

Executing the above using :

postClass c = new postClass();
c.TestData_CM(1000);

I have flex queue enabled so can have 100 batch jobs with holding status. Please share some ideas on this

Thanks

1 Answer 1

7

You can't store an unlimited amount of data within your class' memory; this error means you've exceeded your limit. Instead, you'll need to flip this problem around: make the caseTriggerBatch an Iterable object.

public class caseTriggerBatch implements Database.Batchable<String>, Iterable<String>, Iterator<String> {
    public Iterator<String> iterator() {
        return this;
    }

    Integer index, subindex, total, subtotal;
    String[] types;
    public caseTriggerBatch(Integer total) {
        types = '1,2,3,4,5'.split(',');
        this.total = total;
        index = subindex = 0;
        subtotal = types.size();
    }

    public String next() {
        if(subindex==subtotal) {
            index++;
            subindex=0;
        }
        return postBody('ServiceOpen',types[subindex++],'Open');
    }

    public Boolean hasNext() {
        return index < total || subindex < subtotal;
    }

    static String postBody(String a, String b, String c) {
        return a+b+c;
    }

    public Iterable<String> start(Database.BatchableContext context) {
        return this;
    }

    public void execute(Database.BatchableContext context, String[] values) {

    }

    public void finish(Database.BatchableContext context) {

    }
}

This basic framework builds the entire set of iterations in memory, allowing you to process many millions of rows, if necessary. You'll call it in the same basic way you would before:

Database.executeBatch(new caseTriggerBatch(1000));
3
  • Thanks SFDCFOX.. will go through this. Thanks for your time.
    – sf_user
    Sep 18, 2015 at 23:40
  • 1
    sorry for being dumb..but what does return this returns and what is passed to the execute method argument String[] values ..I am sorry if this is basic question but first time I am seeing Batch class implementing Iterable and Iterator so really want to understand it.
    – javanoob
    Sep 19, 2015 at 1:32
  • 2
    It's okay. (a) return this simply returns an Iterable, which the batch class implements. You could have made them separate classes, but it's more cumbersome in the long run. (b) execute will get a list of stings that were generated from the Iterator functions next and hasNext. Take a look at "using custom iterators" in the docs for more info.
    – sfdcfox
    Sep 19, 2015 at 2:07

You must log in to answer this question.

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