function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
madhan bondalapatimadhan bondalapati 

Data insert from CSV into Salesforce via Visualforce

I am CSV file which contains more than 10k records, i have button on the Vf page where it takes csv file and insert into the Salesforce .but will throw error when the count is more than 1000, .So that the reason i went to batch job.Let me know how to pass the CSv data to batch job,and process and insert in to the salesforce.

Below is the Code for records Lessthan 1000 records.Please share if you have code sample for Batch

Visaul force:

<apex:page controller="ExcelData" >
    <apex:form >
        <apex:pageMessages />
            <apex:pageBlock >
                <apex:pageBlockSection columns="3">
                    <apex:inputFile fileName="{!csvAsString}" value="{!csvFileBody}"></apex:inputFile>
                        <apex:commandButton value="ImportAccounData" 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>


Controller: 

public with sharing class ExcelData {
    public String csvAsString { get; set; }
    public Blob csvFileBody { get; set; }
    Public List<Account>AccList{get;set;}
    Public List<String>CsvLines{get;set;}

public ExcelData(){
    CsvLines=new List<String>();
    AccList=new List<Account>();
}
Public void ImportCSVFile(){
    try{
    csvAsString=csvFileBody.toString();
    CsvLines=csvAsString.split('\n');
    
  for(integer i=1;i<CsvLines.size();i++){
      Account acc=new Account();
      List<String> CsvData=CsvLines[i].split(',');
      acc.Name=CsvData[0];
      acc.AccountNumber=CsvData[1];
      acc.Type=CsvData[2];
      acc.Accountsource=CsvData[3];
      acc.Industry=CsvData[4];
       Acclist.add(acc);
  }  
  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);
        }
}
}
</apex:page>
JyothsnaJyothsna (Salesforce Developers) 
Hi,

Please refer the below link for sample code.

http://developer.financialforce.com/customizations/importing-large-csv-files-via-batch-apex/


Let me know if it helps to you.

Best Regards,
Jyothsna