All posts

Upload data using CSV File

This post will help you to upload data using a CSV File. The csv file should contain the actual field names in the first row. The tricky part is to parse the CSV and create a list of sObjects.

Here is the code to create an uploader on VF Page:
<apex:page controller="CsvUploaderClass">
<style type="text/css">
.custPopup{
background-color: white;
border-width: 2px;
border-style: solid;
z-index: 9999;
left: 50%;
padding:10px;
position: absolute;
width: 600px;
margin-left: -250px;
top:100px;
}
.popupBackground{
background-color:grey;
opacity: 0.90;
//filter: alpha(opacity = 50);
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 9998;
}
</style>
<apex:form>
<apex:outputPanel id="uploaderPopUp">
<apex:outputPanel styleClass="popupBackground" layout="block" rendered="{!showPopUp}">
<apex:outputPanel styleClass="custPopup" layout="block" rendered="{!showPopUp}">
<table>
<tr>
<td>Upload your CSV here:</td>
<td><apex:inputFile value="{!csvFileBody}"/></td>
<td><apex:commandButton value="Import Data" action="{!importCSVFile}"/></td>
<td><apex:commandButton value="Cancel" action="{!cancel}"/></td>
</tr>
</table>
</apex:outputPanel>
</apex:outputPanel>
</apex:outputPanel>
</apex:form>

</apex:page>
---------------------------------------------------------------------------
public class CsvUploaderClass
{
public blob csvFileBody{get;set;}
public void importCSVFile()
{
if(csvFileBody!=null)
{
showPopUp = false;
List<List<String>> allFields = CsvParser.parseCSV(csvFileBody.toString(),false);
list<sObject> sObjectList = CsvParser.csvTosObject(allFields, 'OpportunityLineItem');
try
{
insert sObjectList;
}
Catch(Exception e){}
}
}

}

This is how the uploader will look:



Here is the code of CsvParser class:
public class CsvParser
{
//This method accepts the CSV file body converted into String as a parameter and returns the parsed CSV.
public static List<List<String>> parseCSV(String contents, Boolean skipHeaders)
{
List<List<String>> allFields = new List<List<String>>();

contents = contents.replaceAll(',"""',',"DBLQT').replaceall('""",','DBLQT",');
contents = contents.replaceAll('""','DBLQT');

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)
{
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;
}

//This method accepts the parsed CSV received from the above method and the Object name as the parameters and returns
//the list of sObject type which can be inserted directly.
public static list<sObject> csvTosObject(List<List<String>> parsedCSV, string objectType)
{
Schema.sObjectType objectDef = Schema.getGlobalDescribe().get(objectType).getDescribe().getSObjectType();
system.debug(objectDef);
Map<String, Schema.SObjectField> fieldMap = objectDef.getDescribe().fields.getMap();

list<sObject> objects = new list<sObject>();
list<string> headers = new list<string>();

for(list<string> row : parsedCSV)
{
for(string col : row)
{
headers.add(col);
}
break;
}
system.debug('========================= File Column Headers'+headers);

integer rowNumber = 0;
for(list<string> row : parsedCSV)
{
system.debug('========================= Row Index' + rowNumber);
if(rowNumber == 0)
{
rowNumber++;
continue;
}
else
{
sObject thisObj = objectDef.newSobject();
integer colIndex = 0;
for(string col : row)
{
string headerName = headers[colIndex].trim();
system.debug('========================= Column Name ' + headerName);
if(headerName.length() > 0)
{
try
{
//Finding the data type of a field and type casting the CSV value accordingly.
string FielddataType = String.Valueof(fieldMap.get(headerName).getDescribe().getType());
if(FielddataType == 'DOUBLE' || FielddataType == 'CURRENCY')
{
thisObj.put(headerName, Decimal.ValueOf(col.trim()));
}
else if(FielddataType == 'DATE')
{
thisObj.put(headerName, Date.parse(col.trim()));
}
else
{
thisObj.put(headerName, col.trim());
}
}
catch(exception e)
{
system.debug('============== Invalid field specified in header ' + headerName);
}
colIndex++;
}
}
objects.add(thisObj);
rowNumber++;
}
}
return objects;
}

}

First published on Blogger in 2015. Some screenshots and APIs have changed since; the code is kept as written.

  • apex
  • apex csv file uploader
  • csv file upload
  • Salesforce
  • visualforce page csv file uploader