Skip to content

Instantly share code, notes, and snippets.

@mannharleen
Last active November 7, 2018 16:14
Show Gist options
  • Save mannharleen/2d28b865e2c2d2367e97131dbd519259 to your computer and use it in GitHub Desktop.
Save mannharleen/2d28b865e2c2d2367e97131dbd519259 to your computer and use it in GitHub Desktop.
Wrapper class and CSV file upload
<apex:page controller="importDataFromCSVController_mann">
<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="{!Accounts}" var="acc">
<apex:column value="{!acc.acc.name}" />
<apex:column value="{!acc.acc.AccountNumber}" />
<apex:column value="{!acc.acc.Type}" />
<apex:column value="{!acc.acc.Accountsource}" />
<apex:column value="{!acc.acc.Industry }" />
<apex:column headerValue="Status" >
<apex:outputText id="status" value="{!acc.csvStatus}"/>
</apex:column>
</apex:pageblocktable>
</apex:pageBlock>
</apex:form>
</apex:page>
public class importDataFromCSVController_mann {
/*
The controller will read the file uploaded in CSV format (no headers)
and check if the accounts exist in current org. Accordingly update the
status field
*/
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public string csvStatus{get;set;}
public String[] csvFileLines{get;set;}
public string[] csvRecordData;
public List<account> acc_lst;
public Map<String,String> acc_map;
//define a wrapper class
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';
//write you insert statement here (am commenting it to keep my DB clean :))
//insert a;
}
}
}
//wrapperobject to display on VF
public List<wrapAccount> wrapAccount_lst {get;set;}
public List<wrapAccount> getAccounts() {
return wrapAccount_lst;
}
//populate wrapperObject
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>();
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++) { //assuming no header in the CSV
Account a = new Account();
csvRecordData = csvFileLines[i].split(',');
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment