Importing a CSV File To A Table In Salesforce

How to Import CSV File To A Table In Salesforce?

There are many scenarios where we have CSV(comma separated values) which we want to process in our Visualforce pages so that further custom functionality can be achieved.

csv-logo

For this we need to create a Custom controller and a corresponding Visualforce Page.The Data inside the CSV File will be splitted using the .split function somewhat ike this : .split('\n') and we will fill a list using it which we are gonna iterate it in our Visualforce Page.

Follow the code below:-

VISUALFORCE PAGE:

<apex:page controller = “DataController” sidebar=”false” showheader=”false”>
  <apex:form>
    <apex:pagemessages />
    <apex:pageBlock>
      <apex:pageBlockSection columns=”5″>
        <apex:inputFile value=”{!csvFileBody}” filename=”{!csvAsString}” />
        <apex:commandButton value=”Import Account” action=”{!yourCSVFile}”/>
        <apex:commandButton value=”Add Account” action=”{!insertYourAccount}”/>
      </apex:pageBlockSection>
    </apex:pageBlock>
    <apex:pageBlock mode = “inlineEdit”>
      <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 CONTROLLER :

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

Cheers!!!

Popular Salesforce Blogs