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.
}
}