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
Krishna_BeginnerKrishna_Beginner 

URGENT: Time Limit Exception when uploading a csv file of size 1300 records in two batches of 500 size each

Here is the code I am facing performance issue with. My code is working for 500 records with 100 in each batch. I am trying to process the same csv file with 1000+ records with a batch size of 500 and getting "Time limit exceeded" error on visualforce page. We have a requirment to scale this up to 100,000 thousand records or at least to 50,000 records. I have provided only standard fields in the below code. We have totally 32 columns in the csv file that we are uploading through visualforce page UI. A quick help is much appreciated!!!

public void importCSVFile(){
            csvAsString = csvFileBody.toString();
            csvFileLines = csvAsString.split('\n');
            Integer recordCount = csvFileLines.size();
            Integer batchSize = 500;
            Double batchCount = Math.ceil(recordCount/batchSize);
            System.debug('Batch Counts : '+ batchCount);
       try{
            for( Integer i=1; i<=batchCount; i++){
                recordbatch = recordbatch = new List<String>();
                acclist.clear();
                for(Integer j=(i-1)*batchSize+1; j<=(i*batchSize); j++){
                 System.debug('Record Count : '+ j);
                    if(csvFileLines[j] != null){
               
                    Account account = new Account();
                    string[] csvRecordData = csvFileLines[j].split(',');
                    account.Salutation = csvRecordData[7];
                    account.FirstName = csvRecordData[8];
                    account.LastName = csvRecordData[9];
                    account.PersonMailingCountry = csvRecordData[12];
                    account.PersonMailingStreet = csvRecordData[13];
                    account.PersonMailingCity = csvRecordData[14];
                    account.PersonMailingState = csvRecordData[15];
                    account.PersonMailingPostalCode = csvRecordData[16];
                       
                   acclist.add(account);  
              
               }else{
                        break;
                    }
                    System.debug('Batch size executed : '+ j);
               }
                insert acclist;
               
            }
            }catch (Exception e)
            {
                ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'Please make sure input csv file is correct'+e);
                ApexPages.addMessage(errorMessage);
                System.debug('Exception: '+ e);
            }
                   
  }
MaddySinghMaddySingh
Hi Krishna,

if the eror is TIME LIMIT EXCEEDED then  have a look at this post
https://success.salesforce.com/answers?id=90630000000hCnIAAU


If the error is CPU TIME LIMIT EXCEEDED

You get Apex CPU time limit error when the processing time exceeds 10 seconds for the transaction(Synchronous).

Even if yo break it into batches into same transaction, you still get 10 minutes,

One workaround is if the time of insertion of records doesn't matters to you, you can make it an asynchrounous call/future method and break data into smaller chunks.

However I believe you are using a visualforce page to upload the file and you need synchronous processing.

Please check the debug log and see the limits accumulated at the end.

Krishna_BeginnerKrishna_Beginner
Hi,
Thank you very much for quick response.

We are getting TIME LIMIT EXCEEDED error. The exact Error is as below.
"Time limit exceeded
Your request exceeded the time limit for processing."

The second line from the error says exceeded time limit for processing. This is not an error from code perspective even in this case?

Yes. We need synchronous processing. I was getting CPU TIME LIMIT EXCEEDED exception sometime back and it got resolved after breaking the file into batches in apex code.

Thank you!
Krishna
Bryan SosaBryan Sosa
Hi @Krishna_Beginner.

I need to do the same right now. I need to import about 180,000 records or at least 100,000 through the same method you are using. Were you able to solve this problem?

Thanks a lot. Your help would be greatly appreciated.
Bryan Sosa.