The Apex Data Loader is always there when you want to insert records into Salesforce from a CSV file. But, just in case if you don't want your users to install the Apex Data Loader and learn how to use it, then here is a simple example which tells you how to do the same using Visualforce.
Click here to view the demo.
UPDATE:
Step 1:
Download the template from here. Save the file in your desktop. Upload the file into Static Resources with the name "AccountUploadTemplate".
Step 2:
Create an Apex Class named "FileUploader". Paste the code below and save it.
Step 3:
Create a Visualforce Page named "UploadAccounts". Paste the code below and save it.
Step 4:
Download the CSV file from here for test method coverage. Upload the file with the name 'testMethodCSVUpload'
Step 5:
Create an Apex Class as shown below for test coverage.
Screenshot:
Some pointers:
Click here to view the demo.
UPDATE:
- Added special character support [Ex: French characters]
- Added TEST class coverage
Step 1:
Download the template from here. Save the file in your desktop. Upload the file into Static Resources with the name "AccountUploadTemplate".
Step 2:
Create an Apex Class named "FileUploader". Paste the code below and save it.
Step 3:
Create a Visualforce Page named "UploadAccounts". Paste the code below and save it.
Step 4:
Download the CSV file from here for test method coverage. Upload the file with the name 'testMethodCSVUpload'
Step 5:
Create an Apex Class as shown below for test coverage.
Screenshot:
Some pointers:
- You can use only the standard template. Because, that's how we have done the mapping to the columns in excel and the fields in Salesforce. You can modify the mapping and use your own template.
- Allowing the user to choose his own mapping is possible i believe, but may be a bit complex.
- Also, we use a CSV file. So, you may have to use additional criteria if your data values itself have a comma in them (For ex: Billing Street = 'Mumbai, India ') . This would cause problems because Mumbai and India would be considered as seperate values because of the comma in between them.