+ Start a Discussion
swapnil kaleswapnil kale 

Apex heap size too large while creating an .CSV file

Hi,

I am attaching a csv file and following is the code

   

List<Opportunity> listCurrentShowOpportunities = [select Name,OwnerId,AccountId,StageName,Probability,TotalOpportunityQuantity,Amount,Description,CloseDate from opportunity] ;

String csvContent = 'Name,Owner Name,Owner Id,Account Name,Account Id,Stage Name,Probability,Total Opportunity Quantity,Amount,Description,Close Date'+'\n';
      
 for (Opportunity opp : listCurrentShowOpportunities){
        csvContent = csvContent + opp.Name + ',' + opp.Owner.FirstName + opp.Owner.LastName + ',' + opp.OwnerId + ',' + opp.Account.Name + ',' + opp.AccountId + ',' + opp.Show__r.Name + ',' + opp.Show__c + ',' + opp.StageName + ',' + opp.Probability + ',' + opp.Item__c + ',' + opp.Item_Type__c + ',' + opp.Product_Family__c + ',' + opp.TotalOpportunityQuantity + ',' + opp.Sales_Price__c + ',' + opp.Amount + ',' + opp.Description + ',' + opp.CloseDate + '\n';
       }

      Attachment attachment = new Attachment();
      attachment.Body = Blob.valueOf(csvContent);
      attachment.Name = 'csvFile.csv';
      attachment.ParentId = parentid; 
      insert attachment;
Where listCurrentShowOpportunities does not have any limit. I asume it to be size limit of list.
If we consider that I we get about 7000 oppty string csvContent size gets exceeded and I get Apex heap size too large error.
How should I handle this?


 
Krish RavichandranKrish Ravichandran
You need to consider using the "transient" keyword to reduce heap size. And check your filesize for adding attachments in salesforce
https://help.salesforce.com/apex/HTViewHelpDoc?id=collab_files_size_limits.htm
swapnil kaleswapnil kale
I have made Attachment and csvContent transient but no change getting same error.
Ivan Wang 5Ivan Wang 5
hi,do you fixed the problem? I have same error with you