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
raju.Braju.B 

How to create records in salesforce by browsing the csv file from visualforce page.

Hi,

 

I Want a page and controller..

 

My requirement is: I need to browse a csv file from local system..using visualforce page. after browsing csv file,records has to create in a custom object with the values in csv file by clicking a button in visualforce page.

 

 

Please can any one help me..??

 

Thanks & Regards,

 

Raju.b

nagalakshminagalakshmi

Hi Raju, try this code, i think it will be helpful to u.

 

public class FileUploaderlead
{
    public string nameFile{get;set;}
    public Blob contentFile{get;set;}
    String[] filelines = new String[]{};
    List<lead> accstoupload;
    
    public Pagereference ReadFile()
    {
        nameFile=contentFile.toString();
        filelines = nameFile.split('\n');
        accstoupload = new List<lead>();
        for (Integer i=1;i<filelines.size();i++)
        {
            String[] inputvalues = new String[]{};
            inputvalues = filelines[i].split(',');
            
            lead a = new lead();
            a.lastName = inputvalues[0];
            a.company = inputvalues[1];       
            a.street = inputvalues[2];       
            a.City = inputvalues[3];
            a.State = inputvalues[4];
            a.PostalCode = inputvalues[5];
            a.Country = inputvalues[6];

            accstoupload.add(a);
        }
        try{
        insert accstoupload;
        }
        catch (Exception e)
        {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
            ApexPages.addMessage(errormsg);
        }    
        return null;
    }
    
    public List<lead> getuploadedAccounts()
    {
        if (accstoupload!= NULL)
            if (accstoupload.size() > 0)
                return accstoupload;
            else
                return null;                    
        else
            return null;
    }            
}

 

<apex:page sidebar="false" controller="FileUploaderlead">
   <apex:form >
      <apex:sectionHeader title="Upload data from CSV file"/>
      <apex:pagemessages />
      <apex:pageBlock >
             <center>
              <apex:inputFile value="{!contentFile}" filename="{!nameFile}" /> <apex:commandButton action="{!ReadFile}" value="Upload File" id="theButton" style="width:70px;"/>
            <!--  <br/> <br/> <font color="red"> <b>Note: Please use the standard template to upload Accounts. <a href="{!URLFOR($Resource.AccountUploadTemplate)}" target="_blank"> Click here </a> to download the template. </b> </font>-->
             </center>  
      
      
      <apex:pageblocktable value="{!uploadedAccounts}" var="acc" rendered="{!NOT(ISNULL(uploadedAccounts))}">
          <apex:column headerValue="Account Name">
              <apex:outputField value="{!acc.lastName}"/>
          </apex:column>
          <apex:column headerValue=" Street">
              <apex:outputField value="{!acc.Street}"/>
          </apex:column>
          <apex:column headerValue="City">
              <apex:outputField value="{!acc.City}"/>
          </apex:column>
          <apex:column headerValue="State">
              <apex:outputField value="{!acc.State}"/>
          </apex:column>
          <apex:column headerValue=" Postal Code">
              <apex:outputField value="{!acc.PostalCode}"/>
          </apex:column>
          <apex:column headerValue=" Country">
              <apex:outputField value="{!acc.Country}"/>
          </apex:column>
      </apex:pageblocktable>
      
      </apex:pageBlock>       
   </apex:form>   
</apex:page>

sp08830sp08830

this code work fine except when there are commas in the data itself. say company name is "Acme Computer, Inc." Does anyone know how to fix that problem?

Abhi_TripathiAbhi_Tripathi

Thanks alot Nagalaxmi...you saved my time....!!!

 

cheers

Regards 

Abhi