0

I am trying to upload a document that is less than 1MB in size but it has 5000+ records with 6 columns of fields, I am getting below error

Regex too complicatedError is in expression '{!ReadFile}' in component <apex:commandButton> in page importcsvfilepage: Class.ImportCSVFile.ReadFile: line 11, column 1

when I try to upload with 130KB file of 800 records not getting any error. As we have 6MB is the max size we can upload why am I getting this error?

when I upload a file records will create.

VF Page:

<apex:page sidebar="false" controller="ImportCSVFile">
 <apex:form >
  <apex:sectionHeader title="Upload CSV data File"/>
 
  <apex:pageBlock >
         <center>
          <apex:inputFile value="{!FileContent}" filename="{!Filename}" /> <apex:commandButton action="{!ReadFile}" value="Upload File" id="theButton" style="width:70px;"/>
          <br/> <br/> <font color="red"> <b>Note: Please use the standard template to upload Accounts.</b> </font>
         </center> 
  
  </apex:pageBlock>       
</apex:form>   

</apex:page>

Apex class:

public class ImportCSVFile
{
public string Filename{get;set;}
public Blob FileContent{get;set;}
String[] filelines = new String[]{};
    List<Account> accstoupload=  new List<Account>();

public Pagereference ReadFile()
{
    Filename=FileContent.toString();
    filelines = Filename.split('\n');
    
    for (Integer i=1;i<filelines.size();i++)
    {
        String[] inputvalues = new String[]{};
            inputvalues = filelines[i].split(',');
        
        Account a = new Account();
        a.Name = inputvalues[0];
        a.ShippingStreet = inputvalues[1];
        a.ShippingCity = inputvalues[2];
        a.ShippingState = inputvalues[3];
        a.ShippingPostalCode = inputvalues[4];
        a.ShippingCountry = inputvalues[5];
        accstoupload.add(a);
    }
    try{
        insert accstoupload;
    }
    catch (Exception e)
    {
        ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
        ApexPages.addMessage(errormsg);
    }
    return null;
}

public List<Account> getuploadedAccounts()
{
    if (accstoupload!= NULL)
        if (accstoupload.size() > 0)
        return accstoupload;
    else
        return null;
    else
        return null;
   }

   }

What is the max size file or No of records in a File I can upload without any error?

I need to understand the main reason for these errors when we upload data.

3

The Heap Limit, like other governor limits, is a cumulative problem. So, let's take a look at the problem:

// Doubles the size of the heap
Filename=FileContent.toString();
// More than doubling the size of the heap (+4 bytes per newline)
filelines = Filename.split('\n');

Just in these two lines of code, 130kb worth of file results in at least 520kb worth of Heap Size used. After that, you're locking up that data into small parts of data spread across sObject records, so you're already at least at 1MB of Heap Size used.

You can reduce this use by not using temporary variables. In addition, using the class-level variables will result in View State errors, so this needs to be fixed. This should be changed to a local scope.

Here's a revised version of your code:

public class ImportCSVFile {
    public Blob FileContent { get; set; }

    public Pagereference ReadFile() {
        Account[] records = new Account[0];
        Boolean skipHeader = true;
        for(String line: FileContent.toString().split('\n')) {
            if(skipHeader) {
                skipHeader = false;
                continue;
            }
            {
                String[] fields = line.split(',');
                records.add(
                    new Account(
                        Name = fields[0],
                        ShippingStreet = fields[1],
                        ShippingCity = fields[2],
                        ShippingState = fields[3],
                        ShippingPostalCode = fields[4],
                        ShippingCountry = fields[5]
                    )
                );
            }
        }
        try {
            insert accstoupload;
        } catch (Exception e) {
            ApexPages.addMessage(
                new ApexPages.Message(
                    ApexPages.severity.ERROR,
                    'An error has occured. Please check the template or try again later'
                )
            );
        }
    }

    public List<Account> getuploadedAccounts() {
        return accstoupload != NULL && accstoupload.size() > 0? accstoupload: null;
    }
}

Note that you'll still be limited to much less than 6MB of heap, because data needs to be parsed out. However, this should get you started with your efforts.

Note also that this is not the proper way to parse a CSV file. Expect users to have problems with correct data input.

2
  • Hi @sfdcfox, I am still getting the same error at for(String line: FileContent.toString().split('\n')) {
    – Frank
    Dec 10 '20 at 11:13
  • @Frank If you've already optimized as much as you can, then you're stuck. You'll want to consider client-side parsing instead.
    – sfdcfox
    Dec 10 '20 at 11:47

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.