code: <apex:page controller="importDataFromCSV"> <apex:form > <table class="table table-bordered table-striped"> <apex:pagemessages /> <apex:pageBlock > <apex:pageBlockSection columns="4"> <apex:inputFile value="{!csvFileBody}" filename="{!csvAsString}"/> <apex:commandButton value="Import Account" action="{!importCSVFile}"/> </apex:pageBlockSection> <thead class="head" > <th><b>Name}</b></th> <th><b>AccountNumber</b></th> <th><b>Type</b></th> <th><b>Accountsource</b></th> <th><b>Industry</b></th> </thead> <apex:repeat value="{!Accounts}" var="acc"> <tr> <td>{!acc.acc.name}</td> <td>{!acc.acc.AccountNumber}</td> <td>{!acc.acc.Type}</td> <td>{!acc.acc.Accountsource}</td> <td>{!acc.acc.Industry}</td> <apex:column headerValue="Status" > <apex:outputText id="status" value="{!acc.csvStatus}"/> </apex:column> <td> <apex:commandLink StyleClass="btn btn-secondary" action="{!EditAccount}" reRender="form"> <i class="fa fa-edit" style="font-size:24px"></i> <apex:param name="accountid" value="{!acc.Id}" assignTo="{!selectedAccountId}"/> </apex:commandLink> </td> </tr> </apex:repeat> </apex:pageBlock> </table> </apex:form></apex:page> class: public class importDataFromCSV { public Blob csvFileBody{get;set;} //geter and setter methods for VF page access public string csvAsString{get;set;} public string selectedAccountId { get; set; } public String[] csvFileLines; public string[] csvRecordData; public List<account> acc_lst; public Map<String,String> acc_map; public class wrapAccount { public Account acc {get;set;} public String csvStatus {get;set;} public wrapAccount(Account a, Map<String,String> acc_map) { acc = a; if(acc_map.containsKey(a.Name)) { csvStatus = 'Duplicate'; } else { csvStatus = 'Unique'; insert a; } } } public void LoadData() { acc_lst = [SELECT Name,AccountNumber,Type,Accountsource,Industry FROM Account]; } public List<wrapAccount> wrapAccount_lst {get;set;} public List<wrapAccount> getAccounts() { return wrapAccount_lst; } public pagereference importCSVFile() { csvAsString = csvFileBody.toString(); csvFileLines = csvAsString.split('\n'); system.debug('csvFileLines= '+csvFileLines+' csvFileLines.size() = '+csvFileLines.size()); //get all accounts acc_lst = new List<account>([select Name, Accountnumber from account]); //initialize the map acc_map = new Map<String,String>(); //populate the map for (account a: acc_lst) { system.debug('a = '+a); acc_map.put(a.Name,a.AccountNumber); } wrapAccount_lst = new List<wrapAccount>(); for(Integer i=0;i<csvFileLines.size();i++) { //read the csv row csvRecordData = csvFileLines[i].split(','); //get values from csv into the account Account a = new Account(); a.name = csvRecordData[0] ; a.accountnumber = csvRecordData[1]; a.Type = csvRecordData[2]; a.AccountSource = csvRecordData[3]; a.Industry = csvRecordData[4]; wrapAccount_lst.add(new wrapAccount(a,acc_map)); } return null; } public PageReference EditAccount() { System.debug('selectedAccountId' +selectedAccountId); if (selectedAccountId == null) { return new PageReference('/'); }else{ return new PageReference('/apex/importedit?Id='+selectedAccountId); } } }
@* Salesforce Administrators *@* Salesforce Developers *@Trailblazer Community Cove@* Trailhead Academy *@Ladies Be Architects@SkyVisualEditor: WYSIWYG for Visualforce@Chatter Answers EOL
Hi @Vidhya P
<apex:page controller="importDataFromCSV">
<apex:form >
<table class="table table-bordered table-striped">
<apex:pagemessages />
<apex:pageBlock >
<apex:pageBlockSection columns="4">
<apex:inputFile value="{!csvFileBody}" filename="{!csvAsString}"/>
<apex:commandButton value="Import Account" action="{!importCSVFile}"/>
</apex:pageBlockSection>
<thead class="head" >
<th><b>Name</b></th>
<th><b>AccountNumber</b></th>
<th><b>Type</b></th>
<th><b>Accountsource</b></th>
<th><b>Industry</b></th>
</thead>
<apex:repeat value="{!Accounts}" var="acc">
<tr>
<td>{!acc.acc.name}</td>
<td>{!acc.acc.AccountNumber}</td>
<td>{!acc.acc.Type}</td>
<td>{!acc.acc.Accountsource}</td>
<td>{!acc.acc.Industry}</td>
<td><apex:outputText id="status" value="{!acc.csvStatus}"/> </td>
<td>
<apex:commandLink StyleClass="btn btn-secondary" action="{!EditAccount}" reRender="form">
<i class="fa fa-edit" style="font-size:24px"></i>
<apex:param name="accountid" value="{!acc.acc.Id}" assignTo="{!selectedAccountId}"/>
</apex:commandLink>
</td>
</tr>
</apex:repeat>
</apex:pageBlock>
</table>
</apex:form>
</apex:page>
public class importDataFromCSV {
public Blob csvFileBody{get;set;} //geter and setter methods for VF page access
public string csvAsString{get;set;}
public string selectedAccountId { get; set; }
public String[] csvFileLines;
public string[] csvRecordData;
public List<account> acc_lst;
public Map<String,String> acc_map;
public class wrapAccount {
public Account acc {get;set;}
public String csvStatus {get;set;}
public wrapAccount(Account a, Map<String,String> acc_map) {
acc = a;
if(acc_map.containsKey(a.Name)) {
csvStatus = 'Duplicate';
} else {
csvStatus = 'Unique';
insert a;
}
}
}
public void LoadData() {
acc_lst = [SELECT Name,AccountNumber,Type,Accountsource,Industry FROM Account];
}
public List<wrapAccount> wrapAccount_lst {get;set;}
public List<wrapAccount> getAccounts() {
return wrapAccount_lst;
}
public pagereference importCSVFile()
{
csvAsString = csvFileBody.toString();
csvFileLines = csvAsString.split('\n');
system.debug('csvFileLines= '+csvFileLines+' csvFileLines.size() = '+csvFileLines.size()); //get all accounts
acc_lst = new List<account>([select Name, Accountnumber from account]); //initialize the map
acc_map = new Map<String,String>(); //populate the map
for (account a: acc_lst) {
system.debug('a = '+a);
acc_map.put(a.Name,a.AccountNumber);
}
wrapAccount_lst = new List<wrapAccount>();
for(Integer i=0;i<csvFileLines.size();i++) { //read the csv row
csvRecordData = csvFileLines[i].split(','); //get values from csv into the account
Account a = new Account();
a.name = csvRecordData[0] ;
a.accountnumber = csvRecordData[1];
a.Type = csvRecordData[2];
a.AccountSource = csvRecordData[3];
a.Industry = csvRecordData[4];
wrapAccount_lst.add(new wrapAccount(a,acc_map));
}
return null;
}
public PageReference EditAccount() {
System.debug('selectedAccountId' +selectedAccountId);
if (selectedAccountId == null) {
return new PageReference('/');
}else
{
return new PageReference('/apex/importedit?Id='+selectedAccountId);
}
}
}