Here
is the way to load your data without using Apex Dataloader or any other third
party tool..
Before that create one csv file with the following column names,
-------------------- Download Template from here Contact Template --------------------------
Before that create one csv file with the following column names,
-------------------- Download Template from here Contact Template --------------------------
Apex Class:
- public class ContactLoader
- {
- //It holdes the Content of the CSV File which you have uploaded.
- public String nameFile{get;set;}
- //Rendering pageblocks
- public boolean display{get;set;}
- public boolean records{get;set;}
- //Retrieve Content from the CSV File
- public Blob csvFile{get;set;}
- //Read Lines from the CSV File
- String[] filelines = new String[]{};
- //It hold List of Contact Records
- List<Contact> contactInsert=new List<Contact>();
- public ContactLoader()
- {
- display=true;
- records=false;
- }
- public Pagereference ReadFile()
- {
- display=false;
- records=true;
- if(csvFile!=null)
- {
- // Convert the Blob content to String
- nameFile=csvFile.toString();
- //Read the CSV Rows using split method
- filelines = nameFile.split('\n');
- }
- else
- {
- ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Please select a record to upload...!');
- ApexPages.addMessage(errormsg);
- }
- try
- {
- for (Integer i=1;i<filelines.size();i++)
- {
- String[] inputvalues = new String[]{};
- //Read multiple column values within the same row using split method
- inputvalues = filelines[i].split(',');
- Contact cnt= new Contact();
- // inputvalues[0] to inputvalues[3] holdes the index of the CSV file,Read and Map Field from CSV to Salesforce.
- //Note : If you want to insert Extra fields map those fields below and in your CSV file
- cnt.FirstName= inputvalues[0];
- cnt.LastName= inputvalues[1];
- if(String.isNotBlank(inputvalues[2]))
- {
- cnt.AccountId= inputvalues[2];
- }
- else
- {
- cnt.AccountId=null;
- }
- cnt.LeadSource= inputvalues[3];
- //Add uploaded values into List
- contactInsert.add(cnt);
- }
- //insert your Contact Records into Salesforce
- insert contactInsert;
- }
- catch (Exception e)
- {
- //throw Error if user choosen invalid template
- ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Invalid template or file while processing your records...!');
- ApexPages.addMessage(errormsg);
- }
- return null;
- }
- public List<Contact> getuploadedContacts()
- {
- //Below function return the inserted records in pageblock table
- set<id> recordid= new set<id>();
- for(contact ct:contactInsert)
- {
- recordid.add(ct.id);
- }
- list<contact> ctctlist=new list <contact>();
- ctctlist=[select FirstName,LastName,AccountId,LeadSource,Account.Name from Contact where id IN: recordid ];
- if (contactInsert!= NULL)
- {
- if (contactInsert.size() > 0)
- return ctctlist;
- else
- return null;
- }
- else
- {
- return null;
- }
- }
- public pageReference refresh()
- {
- //redirect after pressed Go Back button
- PageReference pageRef= new PageReference('/apex/UploadContacts');
- pageRef.setRedirect(true);
- return pageRef;
- }
- }
Visualforce Page:
- <apex:page sidebar="false" controller="ContactLoader" showHeader="false">
- <apex:form >
- <apex:pagemessages />
- <apex:pageBlock title="Upload Your CSV File" rendered="{!display}" >
- <center>
- Please choose your file to upload : <apex:inputFile value="{!csvFile}" filename="{!nameFile}" /> <br/>
- <br/><br/><apex:commandButton action="{!ReadFile}" value="Insert" id="theButton" style="background:#216BB5;color:white; width:10%;"/>
- </center>
- </apex:pageBlock>
- <apex:pageBlock title="Inserted Records" rendered="{!records}">
- <apex:pageblocktable value="{!uploadedContacts}" var="cnt" rendered="{!NOT(ISNULL(uploadedContacts))}">
- <apex:column headerValue="First Name">
- <apex:outputField value="{!cnt.FirstName}"/>
- </apex:column>
- <apex:column headerValue="Last Name">
- <apex:outputField value="{!cnt.LastName}"/>
- </apex:column>
- <apex:column headerValue="Account Name">
- <apex:outputField value="{!cnt.Account.Name}"/>
- </apex:column>
- <apex:column headerValue="Lead Source">
- <apex:outputField value="{!cnt.LeadSource}"/>
- </apex:column>
- </apex:pageblocktable>
- <center>
- <br/>
- <apex:commandButton value="Go Back" action="{!refresh}" style="background:#216BB5;color:white;width:10%;"/>
- </center>
- </apex:pageBlock>
- </apex:form>
- </apex:page>
Screen Shoots:
Screen
1:
Screen 2:
If my cvs contains a field that is read only for the object, how do I map it? When saving the apex class I get error that field is read only. In my use case I am doing an update to existing record and querying using the external id which is read only. Thanks!
ReplyDelete