Import CSV File Using Visualforce

Generally, we use data loader to import data in Salesforce from CSV file. Nowadays some other tools are also available to load data in salesforce like dataloader.io. Sometimes there is a requirement when end users do not want to use Data Loader. They want some custom page to load data in Salesforce. Then, in that case, we can use custom code to load data in Salesforce from a CSV file.

Also sometimes using a single CSV file we want to load data in multiple objects. Then, in that case, we can not use data loader to load data. In that case, we can use this approach to load data in Salesforce.

In the example below, we are loading data from the CSV file for account objects.

Click here to download a sample CSV file. You can modify the CSV file to insert or import account records. CSV file attached has the following format:

Click on choose file, then select CSV file and then click on ‘Import Account’ button. All records from the CSV file will be inserted on account records. I have commented insert account line. So this example will only show account records on the visualforce page. You can uncomment insert account list line in below code if you want to insert account list.

Visualforce Page:

<apex:page controller="importDataFromCSVController">
    <apex:form >
        <apex:pagemessages />
        <apex:pageBlock >
            <apex:pageBlockSection columns="4"> 
                  <apex:inputFile value="{!csvFileBody}"  filename="{!csvAsString}"/>
                  <apex:commandButton value="Import Account" action="{!importCSVFile}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock >
           <apex:pageblocktable value="{!accList}" var="acc">
              <apex:column value="{!acc.name}" />
              <apex:column value="{!acc.AccountNumber}" />
              <apex:column value="{!acc.Type}" />
              <apex:column value="{!acc.Accountsource}" />
              <apex:column value="{!acc.Industry }" />
        </apex:pageblocktable>
     </apex:pageBlock>
   </apex:form>
</apex:page>

Apex Code:

public class importDataFromCSVController {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public String[] csvFileLines{get;set;}
public List<account> acclist{get;set;}
  public importDataFromCSVController(){
    csvFileLines = new String[]{};
    acclist = New List<Account>(); 
  }
  
  public void importCSVFile(){
       try{
           csvAsString = csvFileBody.toString();
           csvFileLines = csvAsString.split('\n'); 
            
           for(Integer i=1;i<csvFileLines.size();i++){
               Account accObj = new Account() ;
               string[] csvRecordData = csvFileLines[i].split(',');
               accObj.name = csvRecordData[0] ;             
               accObj.accountnumber = csvRecordData[1];
               accObj.Type = csvRecordData[2];
               accObj.AccountSource = csvRecordData[3];   
               accObj.Industry = csvRecordData[4];                                                                             
               acclist.add(accObj);   
           }
        //insert acclist;
        }
        catch (Exception e)
        {
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importin data Please make sure input csv file is correct');
            ApexPages.addMessage(errorMessage);
        }  
  }
}

Output: