0

I have a Batch class in which I am processing 10K records. The requirement is to create a file document in which all these 10K records which are processed should be listed. So, in my execute method I am adding the records one by one to the list and In finish method I am inserting that list. But I am getting Heap size exceeded error in finish method. Since the data in list is huge so finish method is erroring out. Is there any other approach which we can follow?

List<wrapperList> wrapList = new List<WrapperList>();

global List<customObj> start(Database.BatchableContext BC) {
    return [Select Id FROM Obj where status = 'New' LIMIT 10000];
}
 global void execute(Database.BatchableContext BC, List < customObj > scope) {
    
    ....
    wrapList.add(obj);
    ....

}
global void finish(Database.BatchableContext BC) {

....
insert wrapList; // Giving the Heap Size Error
...
}
        

0

Try converting the objects you are trying to insert into String, with JSON.serialize and then appending to a string. And then insert this string to Docuemnt.

It would be better if you can have wrapper class instead of standard objects, because standard objects when serialized have an attributes property which you would probably never need and will unnecessarily increase your string length.

As its a batch, and heapsize would be 12 MB, a string to go beyond 12Mb is unlikely (if you dont select fields which are long text area or text area etc.). Also try to insert only required fields and not all fields.

And your document is in txt format, you will finally have a JSON data in your document which is readable.

11
  • Correct , if I insert in execute method then 10K records will get inserted into an object. We want to create only 1 file record which should have all 10K records information. – sfdc07 Nov 27 '20 at 4:30
  • Just keep a reference of the document at top when the first insertion happens, updated above. Also may I know what type of document are you creating, so that i can help you futrher ? – Nagendra Singh Nov 27 '20 at 4:37
  • We are creating a ContentDocument File in finish method. In this ContentDocument file, we want to write all the 10K record information like show much time each record is taking to process. Basically, we are doing a logging for each record in a file. – sfdc07 Nov 27 '20 at 4:42
  • And the ContentDocument is in which format? – Nagendra Singh Nov 27 '20 at 4:46
  • Its in .txt format – sfdc07 Nov 27 '20 at 4:48
0

The value in requestString Variable is not retained across multiple execute()

global with sharing class myClass implements Database.Batchable < sObject >, Database.Stateful {
    
    public Class Log{
        String timestamp;
        String operation;
        String additionalrecordInfo;
        
        public Log(DateTime timestamp, String operation, String additionalrecordInfo){
            this.timestamp = timestamp.format('yyyy/MM/dd hh:mm:ss');
            this.operation = operation;
            this.additionalrecordInfo = additionalrecordInfo;
        }
    }
    
    String requestString = '';

    global List<customObj> start(Database.BatchableContext BC) {
        return [Select Id FROM Obj where status = 'New' LIMIT 10000];
    }
     global void execute(Database.BatchableContext BC, List < customObj > scope) {
        
        ....
        new HelperClass().formRequest(... , requestString);
        ....

    }
    global void finish(Database.BatchableContext BC) {
        system.debug('====' + requestString);// Giving Blank
    }
}
class HelperClass{
    void formRequest(..., String requestString){
        myClass.Log l = new myClass.Log(DateTime.Now(),'Contacts upserted':, 'Other info');
        requestString += JSON.serializePretty(l);
    }
}
4
  • Update this as part of your question , as this is not an answer. Also this is not how string works, try to return a string from formRequest and keep on appending to requestString – Nagendra Singh Nov 27 '20 at 6:22
  • You're AWESOME.. Earlier when I tried retaining the values in a list, I never returned the value from Helper. thought, may be it is taking the reference automatically, but yes, after returning it worked. I just tried with 5 records. It is showing the log in very nice readable form. Its very helpful, Thank you so much..! Highly Appreciated it. Let me try with 10K records and see if Heap size error is coming or not. This may take hours to run. Will inform you as soon as I get the log. – sfdc07 Nov 27 '20 at 6:43
  • @NagendraSingh - Storing the data into a string variable did help. It is not throwing the Heap size error anymore! Thank you so much for helping me in fixing that error. But just wondering, what was the issue with List? Why storing the data in List errored out but storing the same info in String variable worked? – sfdc07 Nov 28 '20 at 4:21
  • That's a concept of serialization (Basically why objects take more space in Heap Memory than String) which you can google. – Nagendra Singh Nov 28 '20 at 4:34

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.