I have an object A whose records have to be exported in .dat file format. I have a custom setting with the following three fields: Name (Standard field), Field API Name and Sort Order.

All the fields of object A which has to be displayed on the .dat file are created as Custom Setting Records. Sort order indicates the position of the field in the file. There are more than 100 records in this Custom Setting.

There is a nested for loop used while generating the .dat file which is hitting Apex CPU time limit exceeded exception when there are more around 500 records to be processed on object A.

Code skeleton is as below:

for (A__c objA : lstARecords) {

    for(Iteration over custom setting records) {

        /* Get the API names from the custom setting and its value from the object A's
           record and then keep populating the values on a String variable 
           using which we will be creating a .dat file */

Please guide me to avoid this nested for loop.

  • Did you consider using batch apex to implement this? May 30, 2017 at 7:21
  • No. It is a VF page controller. And we need this functionality in synchronous process only. May 30, 2017 at 7:26
  • It would be a lot easier if you showed the code in the for loop. Although I can see an optimization right now - in your comments you look like you are getting the custom setting inside the loop - for 500 records, that's 500 times you are getting that setting.... hmm that may not be the case. In any case, please show more code. May 30, 2017 at 7:55
  • One optimization you could use would be to create a few concatenated fields on the object - so you'd have less string manipulation to do in the loop. If the formulas are helping, you could set them via trigger. May 30, 2017 at 7:58

1 Answer 1


It's hard to give a clear recommendation due to the minimal example given but there are two things changes that can greatly improve the performance of your code.

  1. Avoid excessive string concatenating. Create a List<String> collection and append to it inside the loop. Outside the loop, use String.join(iterableObj, separator).
  2. There a many ways to solve the sort order problem but one way would be to create a list with the index being the sort order and the values being the field. I.e.

    String[] fields = new String[]{'First_Field__c','Second_Field__c'};

    You can now iterate over this collection for each record, processing the fields in the order defined by the list. You might need to use objA.get('First_Field__c').

  • The suggestion #1 worked. Thanks. However, #2 is already in place in my code. Sort order has been taken from the custom setting. May 30, 2017 at 9:20
  • Sorry that it wasn't clear but #2 was meant to imply that the sortorder collection is built from custom settings before the loop. As you don't want to be doing too much unnecessary iteration. May 30, 2017 at 9:46
  • Okay. Actually, there is a field on custom setting named 'Sort order' based on this order only we need to place the fields on the file. So, currently we are querying the custom setting records ORDER BY Sort Order. May 30, 2017 at 11:09

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.