function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Arjun y 7Arjun y 7 

Receiving heap size limit for the below code

Hi All,

I am recieving heap size limit for the below code. I want to send 25000 records as csv via email. 
 
List<Account > acclist = [Select id,name , CreatedDate , lastModifiedDate from Account];
string header = 'Record Id, Name , Created Date, Modified Date \n';
string finalstr = header ;
for(Account a: acclist)
{
 /* Here I am getting heap size limit error*/     
 string recordString = a.id+','+a.Name+','+a.CreatedDate+','+a.LastModifiedDate +'\n';
       finalstr = finalstr +recordString;
}
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(finalstr);
string csvname= 'Account.csv';
csvAttc.setFileName(csvname);
csvAttc.setBody(csvBlob);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] toAddresses = new list<string> {'pravinl059@gmail.com'};
String subject ='Account CSV';
email.setSubject(subject);
email.setToAddresses( toAddresses );
email.setPlainTextBody('Account CSV ');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

 
Best Answer chosen by Arjun y 7
cloudSavvyProgcloudSavvyProg
Hey,

Sorry, didnt see that line.

The possible solution i could think of is to use String[] and then to use string.join function to get string which can then be used in BLOB.
Something like this:

String[] tempList = new String[];
for(Account a: acclist)
{
 string recordString = a.id+','+a.Name+','+a.CreatedDate+','+a.LastModifiedDate +'\n';
 tempList.add(recordString); 
}

String finalJoinedString = String.join(tempList, ',');  //use the seperator you want.
finalstr = finalstr +finalJoinedString;

blob csvBlob = Blob.valueOf(finalstr);

Doc to help: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm#apex_System_String_join


Hope this helps.


Regards,
CloudSavvyProg

 

All Answers

cloudSavvyProgcloudSavvyProg
Hi Arjun,

Which line is this error occuring?
The error usually means the query result is blowing up the collection. So I am guessing, this following line is the one throwing error.

List<Account > acclist = [Select id,name , CreatedDate , lastModifiedDate fromAccount];

if so, use the filters to the query. Add some conditions in where clause of the above query to retrieve limited the number of records or add Limit clause to the query. This should resolve the issue.

Hope this helps.

Regards,
cloudSavvyProg
Arjun y 7Arjun y 7
Hi,

At line no 6: I have mentioned in comments.

The total records present in query is 25000 records. I want all of those.

Thansks
cloudSavvyProgcloudSavvyProg
Hey,

Sorry, didnt see that line.

The possible solution i could think of is to use String[] and then to use string.join function to get string which can then be used in BLOB.
Something like this:

String[] tempList = new String[];
for(Account a: acclist)
{
 string recordString = a.id+','+a.Name+','+a.CreatedDate+','+a.LastModifiedDate +'\n';
 tempList.add(recordString); 
}

String finalJoinedString = String.join(tempList, ',');  //use the seperator you want.
finalstr = finalstr +finalJoinedString;

blob csvBlob = Blob.valueOf(finalstr);

Doc to help: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm#apex_System_String_join


Hope this helps.


Regards,
CloudSavvyProg

 
This was selected as the best answer