Monday 19 June 2017

Import CSV file using apex controller.

1. We will import CSV file data using apex controller. Now import the data of Account object custom controller and display data on to visualforce page.

2. Normally we use data loader to import data from CSV file in salesforce. And we can some other tools to loads the data in in salesforce like Jitterbit data loader. 

3. Sometimes end users don't want to use Apex data loader to import data in salesforce. So we use custom page to load the data and write the custom code to load the data in salesforce.

4. If we want to import data in to multiple objects using single CSV file, in that case we can not use any data loaders to load data. In that case we can use custom code to load the data in Salesforce.

In this example , I am loading the data from CSV file in to Account Object.

Attach any csv file with these type of fields.

CSV File-





Click For Demo




Here , I am displaying account records only, If you want insert the csv data in to accounts then please uncomment the insert code.

Visualforce Page:

<apex:page controller="importCSVDataController" sidebar="false">
    <apex:form >
        <apex:pagemessages />
        <apex:pageBlock >
            <apex:pageBlockSection columns="4"> 
                  <apex:inputFile value="{!csvBodyData}"  filename="{!csvInString}"/>
                  <apex:commandButton value="Import Account" action="{!importCSVFile}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock >
           <apex:pageblocktable value="{!accounts}" 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 Class:

public class importCSVDataController 
{
  public String csvInString{get;set;}
  public Blob csvBodyData{get;set;}
  public String[] readCSVData{ge
t;set;}
  public List<Account> accounts{get;set;}
  
  public importCSVDataController ()
  {
    readCSVData = new String[]{};
    accounts = New List<Account>(); 
  }
  
  public void importCSVFile()
  {
       try
       {
           csvInString = csvBodyData.toString();
           System.debug('csvInString='+csvInString);
           readCSVData = csvInString.split('\n'); 
           System.debug('readCSVData='+readCSVData);
            
           for(Integer i=1;i<readCSVData.size();i++)
           {
               Account accObj = new Account() ;
               string[] csvAccountData = readCSVData[i].split(',');
               accObj.name = csvAccountData[0] ;             
               accObj.accountnumber = csvAccountData[1];
               accObj.Type = csvAccountData[2];
               accObj.AccountSource = csvAccountData[3];   
               accObj.Industry = csvAccountData[4];                                                                             
               accounts.add(accObj);   
           }
            //insert accounts;
        }
        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);
        }  
  }
}

1 comment:

  1. Sir thank you for sharing this code, i am getting Apex CPU time limit exceed error when data is large(3000 records or more), how to fix it?

    ReplyDelete

If you have any doubts, please let me know.