Last active
March 22, 2019 12:22
-
-
Save yasarshaikh/923730a9a8db6799bba203c51222b698 to your computer and use it in GitHub Desktop.
Read CSV file and upload data to the system without Saving the CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FirstName | LastName | Company | |
---|---|---|---|
test1 | 1test | 1comp1 | |
test2 | 2test | 2comp2 | |
test3 | 3test | 3comp3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Upload a file and put it in your personal documents folder--> | |
<!-- Page: --> | |
<apex:page standardController="Document" extensions="UploadExcelController" showHeader="false"> | |
<apex:messages /> | |
<apex:form id="theForm"> | |
<apex:pageBlock > | |
<apex:pageBlockSection > | |
<apex:inputFile value="{!document.body}" filename="{!document.name}"/> | |
<apex:commandButton value="Save" action="{!save}"/> | |
</apex:pageBlockSection> | |
</apex:pageBlock> | |
</apex:form> | |
</apex:page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UploadExcelController { | |
Document d{get;set;} | |
public UploadExcelController(ApexPages.StandardController controller) { | |
d = (Document) controller.getRecord(); | |
d.folderid = UserInfo.getUserId(); //this puts it in My Personal Documents | |
} | |
public void save(){ | |
Lead lobj; | |
List<Lead> lLead = new List<lead>(); | |
System.debug('Doc :' + d); | |
if(d.body != NULL) | |
{ | |
String sCSV = EncodingUtil.base64Decode(EncodingUtil.base64Encode(d.body) ).toString(); | |
System.debug('Body of Doc : ' + sCSV); | |
List<String> lstFieldNames = new List<String>(); | |
List<String> dataLines = sCSV.split('\\n'); | |
Map <String, Integer> fieldNumberMap = new Map < String, Integer > (); | |
string[] csvFieldNames = dataLines[0].split(','); | |
for (Integer i = 0; i < csvFieldNames.size(); i++) { | |
fieldNumberMap.put(csvFieldNames[i], i); | |
lstFieldNames.add(csvFieldNames[i].trim()); | |
} | |
system.debug('dataLines ' +dataLines); | |
for (Integer i = 1; i < dataLines.size(); i++) { | |
lobj = new Lead(); | |
string[] csvRecordData = dataLines[i].split(','); | |
for (String fieldName: csvFieldNames) { | |
Integer fieldNumber = fieldNumberMap.get(fieldName); | |
String fieldValue = csvRecordData[fieldNumber]; | |
lobj.put(fieldName.trim(), fieldValue.trim()); | |
} | |
lLead.add(lobj); | |
} | |
} | |
System.debug('Before insert '); | |
if(lLead.size() > 0 ){ | |
insert lLead; | |
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Added successfully!'); | |
ApexPages.addMessage(myMsg); | |
} | |
System.debug('After insert'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment