Upload Contact Records Using Apex in bulk format

VF:
----
<apex:page controller="UploadRecords" sidebar="false" showHeader="true">
    <apex:form >
        <apex:pageBlock >        
        <apex:pageBlockSection columns="3">
        <marquee>Choose Upload File</marquee>
        <apex:inputFile value="{!contentFile}" fileName="{!nameFile}"></apex:inputFile>
        <apex:commandButton value="Upload" action="{!ReadFile}"/>
        </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock >
            <apex:pageBlockSection columns="1">
                 <apex:pageBlockTable value="{!conn}" var="c">
                     <apex:column value="{!c.lastname}"/>
                     <apex:column value="{!c.phone}"/>
                     <apex:column value="{!c.email}"/>
                 </apex:pageBlockTable>   
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex :
------
public class UploadRecords{
    public string nameFile{get;set;}
    public Blob contentFile{get;set;}
    String[] filelines = new String[]{};    
    List<Contact> accstoupload;    
    public Pagereference ReadFile(){
        nameFile=contentFile.toString();
        filelines = nameFile.split('\n');
        accstoupload = new List<Contact>();                
        for (Integer i=1;i<filelines.size();i++){
            String[] inputvalues = new String[]{};
            inputvalues = filelines[i].split(',');            
            Contact a = new Contact();
            a.lastName = inputvalues[0];
            a.phone=inputvalues[1];
            a.email=inputvalues[2];            
            accstoupload.add(a);
        }        
        insert accstoupload;        
        return null;
    }       
    Public List<Contact> getConn(){
        return accstoupload;
    }
}

No comments:

Featured

What is Cryptography in salesforce and what are all the algorithms provided by them ?

A). It is a security protocal between two systems. Lets say we are integration two systems without any encrytion mechanism then hackers wil...

Popular