如何在Salesforce中解析CSV

Sid*_*idC 1 salesforce apex-code

我有一个CSV,其中包含需要转到多个Salesforce自定义对象的字段.我已经创建了APEX类来处理解析.但是,当我从VisualForce页面调用该类时,我收到消息:

Collection size 3,511 exceeds maximum size of 1,000.
Run Code Online (Sandbox Code Playgroud)

我的APEX课程如下(从关于该主题的2篇博文中拼凑而成):

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<List<String>> getResults() {        
 List<List<String>> parsedCSV = new List<List<String>>();        
 rowCount = 0;        
 colCount = 0;        
 if (contentFile != null){            
 String fileString = contentFile.toString();            
 parsedCSV = parseCSV(fileString, false);            
 rowCount = parsedCSV.size();            
 for (List<String> row : parsedCSV){                
 if (row.size() > colCount){                    
 colCount = row.size();                
}            
}        
}        
return parsedCSV;    
}  
public static List<List<String>> parseCSV(String contents,Boolean skipHeaders) {        
List<List<String>> allFields = new List<List<String>>();    
// 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<String> lines = new List<String>();    
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<String> fields = line.split(',');          
List<String> cleanFields = new List<String>();        
String compositeField;        
Boolean makeCompositeField = false;        
for(String field : fields) {            
if (field.startsWith('"') && 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;       
}  
}
Run Code Online (Sandbox Code Playgroud)

如何确保上述类将CSV中的正确列插入到正确的自定义对象中?

由于某些对象包含对其他对象的查找,因此如何确保首先将数据加载到查找字段中,然后再加载到子表中?

最后,我如何解决上面的错误消息?是否建议使用VisualForce页面?

非常感谢您的帮助和指导.

Mat*_*cey 5

出于兴趣,是否需要来自visualforce页面?您可以使用Data Loader加载CSV,甚至可以自己使用批量API构建解决方案.

如果你确实需要从一个页面进行,我建议不要立即拆分新行,而是使用substringindexOf从输入字符串一次只拉一行,然后处理它们.