我有一個CSV,其中包含需要轉到多個Salesforce自定義對象的字段。我創建了APEX類來處理解析。然而,當我打電話從VisualForce頁面類,我收到消息:如何在Salesforce中解析CSV
Collection size 3,511 exceeds maximum size of 1,000.
我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;
}
}
如何我確保上面的類將正確的列從CSV插入正確的自定義對象嗎?
由於一些對象包含其他人的查詢,我如何確保數據首先加載到查找字段中,然後再加載到子表中?
最後,我如何解決上述錯誤消息?除了建議使用VisualForce頁面之外還有其他的東西嗎?
非常感謝您的幫助和指導。
您是如何進行測試的? – Red2678
這裏有太多獨立的問題(「我如何解析CSV?」,「我該如何解決這段特殊的代碼?」,「我應該使用VisualForce頁面以外的東西嗎?」,「我該如何解決這個特定的錯誤?「)讓任何人以對未來讀者有用的方式全面回答。 :( –