0

I am trying to create and upload account record csv file to google drive. I am getting Apex heap size limit while creating csv file body.

global class GoogleDriveBatchClass implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful{
    private string query;
    private string header = 'id, Name , CreatedDate, lastModifiedDate \n';
    private string finalstr;
        global GoogleDriveBatchClass(){
                   // Batch Constructor
                   finalstr = header;
                   query = 'Select id,name , CreatedDate , lastModifiedDate from Account';
        }

        // Start Method

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

      // Execute Logic
       global void execute(Database.BatchableContext BC, List<Account>scope){
                  for(Account a: scope) {
                           string recordString = a.id+','+a.Name+','+a.CreatedDate+','+a.LastModifiedDate +'\n';
                           finalstr +=  recordString;

                    }
                   System.debug('heapsize>>>>>'+Limits.getHeapSize());
                   System.debug('heaplimit>>>>>'+Limits.getHeapSize());
                   System.debug('finalstrsize>>>>>'+finalstr.length());    


                 }

       global void finish(Database.BatchableContext BC){
            System.debug('insidefinish>>>>>');
            System.debug('finalstrsize>>>>>'+finalstr.length());
           cGoogleAppAuthenticationWithSalesforce.UploadFileToGoogleDrive(finalstr);//upload csv file to goole drive finalstr as file body.
       }
    }
4
  • That would seem to tell you that the number of accounts in your org is getting too large to use this method as a single file. You could split it into two files where you get accounts beginning with say Names beginning from A to M and then N to Z If you have numeric characters, those are usually put at the the start of a split file.
    – crmprogdev
    Jun 7, 2016 at 11:59
  • How to be sure that record from A to M will be in heap size limit while more record will insert in future .
    – RaviKant
    Jun 7, 2016 at 12:09
  • Another way to go is to set a maximum number of lines, instead of splitting from A to M. A bit of trial and error could give you a number of lines that are safe.
    – mkorman
    Jun 7, 2016 at 12:56
  • N.B. Your CSV needs to use Apex methods to encode strings as CSV since if an Account name contains a comma or double quote, the CSV will be broken
    – cropredy
    Jun 7, 2016 at 17:49

1 Answer 1

3

"How to be sure that record from A to M will be in heap size limit while more record will insert in future"

You can use getLimitHeapSize()and compare to getHeapSize(). If you do some calculations around the number of records you've queried vs current Heap size, that should give you some idea of how many more you can include before you exceed the heap. You'll of course want to allow extra heap for other things so you don't run over before you check again. Then, at some point, you'll need to cut things off and send your file. If you were to use some kind of stateful method, you could presumably preserve where you were at when you had to cut your file off.

That having been said, what you're doing really isn't a long term solution for back-up of your records. I recommend you look at other means of exporting your Data to create CSV files that are saved to Google Drive such as Apex Data Loader or some other 3rd party solution that might have the capability to save to Google Drive.

You must log in to answer this question.

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