title: How to Parse a CSV in Salesforce
body: <p>I have a CSV that contains fields that need to go to several Salesforce custom objects.  I've created the APEX class to handle the parsing.  However, when I call the class from a VisualForce page, I receive the message:</p>
  
  <pre><code>Collection size 3,511 exceeds maximum size of 1,000.
  </code></pre>
  
  <p>My APEX class is as follows (cobbled together from 2 blog posts on the topic):</p>
  
  <pre><code>public class parseCSV{  
  public Blob contentFile { get; set; }    
  public String nameFile { get; set; }    
  public Integer rowCount { get; set; }    
  public Integer colCount { get; set; }    
  public List&lt;List&lt;String&gt;&gt; getResults() {        
   List&lt;List&lt;String&gt;&gt; parsedCSV = new List&lt;List&lt;String&gt;&gt;();        
   rowCount = 0;        
   colCount = 0;        
   if (contentFile != null){            
   String fileString = contentFile.toString();            
   parsedCSV = parseCSV(fileString, false);            
   rowCount = parsedCSV.size();            
   for (List&lt;String&gt; row : parsedCSV){                
   if (row.size() &gt; colCount){                    
   colCount = row.size();                
  }            
  }        
  }        
  return parsedCSV;    
  }  
  public static List&lt;List&lt;String&gt;&gt; parseCSV(String contents,Boolean skipHeaders) {        
  List&lt;List&lt;String&gt;&gt; allFields = new List&lt;List&lt;String&gt;&gt;();    
  // replace instances where a double quote begins a field containing a comma    
  // in this case you get a double quote followed by a doubled double quote    
  // do this for beginning and end of a field    
  contents = contents.replaceAll(',"""',',"DBLQT').replaceall('""",','DBLQT",');    
  // now replace all remaining double quotes - we do this so that we can reconstruct    
  // fields with commas inside assuming they begin and end with a double quote    
  contents = contents.replaceAll('""','DBLQT');    
  // we are not attempting to handle fields with a newline inside of them    
  // so, split on newline to get the spreadsheet rows    
  List&lt;String&gt; lines = new List&lt;String&gt;();    
  try {        
  lines = contents.split('\n');    
  } 
  catch (System.ListException e) {        
  System.debug('Limits exceeded?' + e.getMessage());    
  }    
  Integer num = 0;    
  for(String line : lines) {        
  // check for blank CSV lines (only commas)        
  if (line.replaceAll(',','').trim().length() == 0) break;
  List&lt;String&gt; fields = line.split(',');          
  List&lt;String&gt; cleanFields = new List&lt;String&gt;();        
  String compositeField;        
  Boolean makeCompositeField = false;        
  for(String field : fields) {            
  if (field.startsWith('"') &amp;&amp; field.endsWith('"')) 
  {
              cleanFields.add(field.replaceAll('DBLQT','"'));            
  } 
  else if (field.startsWith('"')) {                
  makeCompositeField = true;                
  compositeField = field;            
  }
  else if (field.endsWith('"')) {
  compositeField += ',' + field;                
  cleanFields.add(compositeField.replaceAll('DBLQT','"'));                
  makeCompositeField = false;            
  }
  else if (makeCompositeField) {                
  compositeField +=  ',' + field;            
  }
  else {
  cleanFields.add(field.replaceAll('DBLQT','"'));            
  }        
  }                
  allFields.add(cleanFields);    
  }    
  if (skipHeaders) allFields.remove(0);    
  return allFields;       
  }  
  }
  </code></pre>
  
  <p>How do I ensure that the above class inserts the correct columns from the CSV into the proper custom object?  </p>
  
  <p>Since some objects contain lookups to others, how do I ensure that data is loaded into the lookup fields first, and then into the child tables?</p>
  
  <p>Finally, How do I work around the error message above?  Is something besides use of a VisualForce page suggested?</p>
  
  <p>Thanks much for your help and guidance.</p>