0

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?

0

I think it's not a List size trouble, simply you should split your csv in more CSVs because you are reaching the maximum heap size for your transaction. (6 MB in Synchronous context) In fact, the String you are generating is probably going over this size.

Here you can find some documentation about heap size Limits:

Governor Limits

  • Can you please give an example how can I split it. How can this be done without using DML statement in for loop? – Swapnil Kale Mar 26 '15 at 10:48
  • I ve just read..do you still need it? – user9959 Mar 27 '15 at 8:21

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.