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
Sales person 375Sales person 375 

Import Accounts and contacts from single csv file using Visualforce and Apex

Hi,

Im new to salesforce, im working on visualforce page to import Account and contacts to salesforce.

here is my requiremnt, please guide me to achive this. 
 
  1. Give the customer ability to import Accounts from external csv file to salesforce with button on custom Visualforce page.
  2. Read the csv file and display the content in the Tabular form in the same Visualforce page.
  3. Verify the Duplicate records based on Phone number, if the phone number of the import record matches with existing Account record then verify for Email field match, if matches then populate error message as Duplicate record on Visualforce page Table under Status column for particular record. Otherwise insert record and update the status column on Visualforce page as Success.
  4. Extend the functionality to multiple Objects (Account, Contact, and Opportunity) from single csv file.
Thanks,
Punith
SandhyaSandhya (Salesforce Developers) 
Hi,

Please refer below links which have sample code for a similar requirement.

http://www.sfdcpoint.com/salesforce/import-csv-file-using-apex-visualforce/

 http://salesforce.stackexchange.com/questions/60241/insert-records-from-a-csv-file-to-saleforce-object-using-visualforce-and-apex
 
http://salesforce.stackexchange.com/questions/11898/data-import-from-csv-using-visualforce-page-things-to-consider
 
Hope this helps you!

Please mark it as Best Answer if my reply was helpful. It will make it available for other as the proper solution.
 
Thanks and Regards
Sandhya

 
Sales person 375Sales person 375
i need to update the status colum on the VF page and only unique recods should be uploaded to Account.

here is my code: im getting error "common.apex.runtime.impl.ExecutionException: List index out of bounds: 5" and status is not getting updated. 

Please help me

VF:
<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:column headerValue="Status" >
              <apex:outputText id="status"/>
              </apex:column>
           </apex:pageblocktable>
        </apex:pageBlock>
   </apex:form>
</apex:page>

Apex Class:

public class importDataFromCSVController {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public string csvStatus{get;set;}
public String[] csvFileLines{get;set;}
public List<account> acclist{get;set;}
public string[] csvRecordData;
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() ;
               csvRecordData = csvFileLines[i].split(',');
               accObj.name = csvRecordData[0] ;            
               accObj.accountnumber = csvRecordData[1];
               accObj.Type = csvRecordData[2];
               accObj.AccountSource = csvRecordData[3];  
               accObj.Industry = csvRecordData[4];
               uploadStatus(accObj);
               csvRecordData[5] = csvStatus;
            }
        insert acclist;
        }
        catch (Exception e)
        {
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'error here');
            ApexPages.addMessage(errorMessage);
        } 
  }
  
public List<Account> uploadStatus(Account a)
   {
       for(Account acc:[SELECT Name, Accountnumber FROM Account])
       {
           if(acc.name == a.name && acc.accountnumber == a.accountnumber)
           {
               this.csvStatus = 'Duplicate';
                system.debug('inside if');
           }
           else
           {
               system.debug('inside else');
               acclist.add(a);
               this.csvStatus = 'unique';
               
           }
       }
       return acclist;
   }
}