Below is my code.

companyList = [
            Select a.Id,a.Name, a.Phone, a.Website, etc... From Account a
            Where a.RecordType.Name = 'Blah'
String csvString = '';
        for(Account company : companyList)
            csvString += myUtil.formatCSVString(company.Id) + ','
                        + myUtil.formatCSVString(company.Name) + ','
                        + myUtil.formatCSVString(company.Phone) + ','


How can I solve this error?

I am not doing any insert or update. I am just only select the data and want to do necessary thing.

More.. I use

1.Use SOQL for loop. Still error.

  1. I can't put the code in future call. As I want string return back form that method.

3.for limitation the of filters, I already use in SOQL statement by using WHERE.

The way I want to do is below. 1) I will query with Where condition from DB. 2) for each record, I need to parse its data by calling my helper method which will return String. 3) for each return string, I will append to CSVstring.

How can I achieve without hitting limitation. I know it is really simple to write in Java, Now I am stuck on this simple loop.

  • 1
    The reason why you're hitting this exception is coz your code is executing more than 20000 statements. If you examine your for loop I see that you're calling the myutil.formatCSVString(string) method lot of times inside the for loop, which in-turn is running the code inside that method causing the 200001 exception. I suggest instead of passing a string to that method, would it be possible to pass the sObject (account) to your method, process the sobject and return the parsed string back the main class?
    – Sam
    Jul 8, 2013 at 6:18
  • hi sameer, I changed like you have said. still error. any idea?
    – kitokid
    Jul 8, 2013 at 6:26
  • 1
    How many accounts are there in the file you are trying to process ? If this is a very big csv file you may want to break it up into smaller files. Jul 8, 2013 at 6:48
  • it is more than 7k.
    – kitokid
    Jul 8, 2013 at 9:06
  • 1
    Have you looked at the String instance method escapeCsv in lieu of myUtil.formatCSVString ? This would avoid the method call and save script statements
    – cropredy
    Jul 8, 2013 at 20:38

2 Answers 2


Creating really big .csv files is a challenge without leveraging the async framework. Try to reduce the calls to that formatCSVString(), or perhaps refactor is such that you only need to call it once per line, rather than one per field.

I second Sdry's point, you may have to break this up into smaller chunks and only process, say, 2000 records at a time. Alternatively, going to the async framework, either through a @future method or a batch job, will get you a limit of 1,000,000 code statements per execution context.

After you construct this .csv string, what are you going to do with it? Can you explain the full use case, end to end, in more detail? What starts off this process and what is the end result?

  • I use @future methods and chop into 2 big chunks by requirements.
    – kitokid
    Aug 23, 2013 at 10:08
  • Interesting adjustment to this, in Winter '14 they are removing the script statement limit! It's being replaced by a limit on the overall time a process can execute. Perhaps your code will run just fine on a Winter '14 build. Aug 27, 2013 at 4:55
  • Curiously, I'm still seeing script limits on the Winter 14 preview orgs and Winter 14 sandboxes. I wonder if they just haven't switched over yet, or are perhaps thinking of changing their mind?
    – kibitzer
    Sep 7, 2013 at 7:14
  • There is a great article on processing large CSV files using Batch Apex here, developer.financialforce.com/customizations/… Sep 19, 2013 at 22:46

You can reduce your string concatentions with String.join which might help a little.

String[] csv = new String[] {};

for(Account company : companyList) {
    String[] csvRow = new String[] {

    csv.add(String.join(csvRow, ','));

String csvString = String.join(csv, '');

You must log in to answer this question.

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