Here is my code
// dynamic fields, the same with queried fields of Database.query(sqlStr)
String fieldList = 'Id, Name, Number, Text__c..........'.split(',');
for(SObject sob : Database.query(sqlStr))
{
String[] rowValues = new String[] {};
for(Integer i = 0, j = fieldList.size(); i < j; i++)
{
String fieldValueStr = String.valueOf(sob.get(fieldList[i]));
rowValues.add(fieldValueStr);
}
String joinedRowValues = String.join(rowValues, ' ▏');
this.values.add(joinedRowValues); // values is from a class property
}
the result will be like this
"values": [
"00190000014IoCoAAK ▏ 00081 ▏ Super Mart ▏ ...",
"00190000014EoCAABK | 0008tator | Test Mart ▏ ...",
......................................
],
If data is more than 5000, there will be lots of loop, for example, we have 30 fields, 5000 records, loop will be 30*5000. Too many loop will cause CPU timeout.
How to reduce this loop and also get the same result?