@Andrei.Z
Here is what I'm talking about:
Here is my new .csv updated:
Employee_Name__c,Employee_Status__c
Erich Schulz,Active
Joe Dohn,Active
Walter Anderson,InActive
David Hudson,InActive
END
I'm have the following code i'm trying to import the .csv file but it does not looks like its not even going through the loop to display the data in <apex:pageblocktable
, so what is happening is that, i browse and select the .csv file and click on upload csv button and does not loop the data and show it in pageblocktable. i have few records as shown below.
I'm following this reference link
I have the following .csv
data file:
Employee Name,Employee Status
Erich Schulz,Active
Joe Dohn,Active
Walter Anderson,InActive
David Hudson,InActive
Visualforce page:
<apex:page controller="importDataFromCSVController" title="Contacts Uploader">
<style>
.controlPanel td{
width:200px;
}
.controlPanel{
margin-top:20px;
}
</style>
<apex:form >
<apex:pageBlock id="controlPanel">
<table class="controlPanel fromFile">
<tr>
<td colspan="2"><b>Upload a CSV file to save its contents as Contact records</b></td>
</tr>
<tr>
<td>Upload CSV file</td><td><apex:inputFile value="{!csvFileBody}" accept="csv" contentType="text/csv" /></td>
</tr>
<tr>
<td> </td><td><apex:commandButton value="Upload CSV" action="{!readFromFile}"/></td>
</tr>
</table>
</apex:pageBlock>
<apex:pageBlock id="dataPanel">
<apex:pagemessages />
<apex:pageblocktable value="{!lstContacts}" var="con">
<apex:repeat value="{!lstFieldNames}" var="fieldName">
<apex:column value="{!con[fieldName]}" />
</apex:repeat>
</apex:pageblocktable>
<apex:commandButton value="Save to Contacts" action="{!saveData}" rendered="{!readSuccess}" />
</apex:pageBlock>
</apex:form>
</apex:page>
Apex code:
public class importDataFromCSVController {
public String documentName {get;set;}
public Blob csvFileBody{get;set;}
public String[] contactDataLines {get;set;}
public List <Employee_Upload__c> lstContacts {get;set;}
public boolean readSuccess {get;set;}
public List<String> lstFieldNames{get;set;}
Employee_Upload__c conObj;
String contactDataAsString;
String fieldValue;
Integer fieldNumber;
Map <String, Integer> fieldNumberMap = new Map < String, Integer > ();
public importDataFromCSVController()
{
documentName = '';
readSuccess = FALSE;
contactDataLines = new String[] {};
lstContacts = new List <Employee_Upload__c> ();
lstFieldNames = new List<String>();
}
public void readFromFile(){
try{
contactDataAsString = csvFileBody.toString();
system.debug('contactDataAsString ' +contactDataAsString );
readCSVFile();
}
catch(exception e){
readSuccess = FALSE;
ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.Severity.ERROR,'Error reading CSV file');
ApexPages.addMessage(errorMessage);
}
}
public void readCSVFile() {
lstContacts.clear();
contactDataLines = contactDataAsString.split('\n');
string[] csvFieldNames = contactDataLines[0].split(',');
for (Integer i = 0; i < csvFieldNames.size(); i++) {
fieldNumberMap.put(csvFieldNames[i], i);
lstFieldNames.add(csvFieldNames[i].trim());
}
system.debug('contactDataLines ' +contactDataLines);
for (Integer i = 1; i < contactDataLines.size(); i++) {
conObj = new Employee_Upload__c();
string[] csvRecordData = contactDataLines[i].split(',');
for (String fieldName: csvFieldNames) {
fieldNumber = fieldNumberMap.get(fieldName);
fieldValue = csvRecordData[fieldNumber];
conObj.put(fieldName.trim(), fieldValue.trim());
}
lstContacts.add(conObj);
}
if(lstContacts.size() > 0){
readSuccess = TRUE;
}
}
public void saveData() {
try {
//INSERT lstContacts;
ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.INFO, 'Contact records inserted successfully');
ApexPages.addMessage(errorMessage);
} catch (Exception e) {
ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR, 'An error has occured while saving data');
ApexPages.addMessage(errorMessage);
}
}
}
contactDataLines
show? (2) addrerender="datapanel"
to your commandButton; (3) as an aside - this is a somewhat fragile csv parser but might be OK for your limited use case (a) column names must be sobject fieldname and b) no support for embedded cell value of comma or line break.apex:inputFile can not be used in conjunction with an action component, apex:commandButton or apex:commandLink that specifies a rerender or oncomplete attribute.
3) do you know any better csv parser? i have added sobject fieldname please see my updated question\n
to\r
, post this as answer.