Activity Forums Salesforce® Discussions How to import CSV file to create a record in sfdc using apex code?

  • Piyush

    Member
    September 23, 2019 at 11:59 am

    Hi Saddam,

     

    <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>
    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);
            }  
      }
    }

    for more you can refer http://www.sfdcpoint.com/salesforce/import-csv-file-using-apex-visualforce/

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos