Last active
February 18, 2025 21:41
-
-
Save emoran/929adb7bd28b0369bc2a to your computer and use it in GitHub Desktop.
Read a CSV file in apex code / visualforce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Corpogas_ReadCSVController { | |
public Blob csvFileBody{get;set;} | |
public string csvAsString{get;set;} | |
public String[] csvFileLines{get;set;} | |
public List<OpeationDirectoryInformation> directory; | |
public List<OpeationDirectoryInformation> getDirectory(){ | |
return this.directory; | |
} | |
public void setDiretory(List<OpeationDirectoryInformation> list_directory){ | |
this.directory = list_directory; | |
} | |
public Corpogas_ReadCSVController(){ | |
csvFileLines = new String[]{}; | |
} | |
public void importCSVFile(){ | |
Set<String> set_account_Information = new Set<String>(); | |
Set<String> set_contact_Information = new Set<String>(); | |
Map<String,String> map_account = new Map<String,String>(); | |
Map<String,String> map_contact = new Map<String,String>(); | |
directory = new List<OpeationDirectoryInformation>(); | |
try{ | |
csvAsString = csvFileBody.toString(); | |
csvFileLines = csvAsString.split('\r'); | |
for(Integer i=1;i<csvFileLines.size();i++){ | |
string[] csvRecordData = csvFileLines[i].split(','); | |
set_account_Information.add(csvRecordData[0]); | |
set_contact_Information.add(csvRecordData[6]); | |
} | |
for(Account accs:[Select Id,No_Estacion__c from Account where No_Estacion__c IN:set_account_Information]){ | |
System.debug('### AccountNumber: '+accs.No_Estacion__c); | |
map_account.put(String.valueOf(accs.No_Estacion__c),accs.Id); | |
} | |
for(Contact contact:[Select Id,Email from Contact where Email IN:set_contact_Information]){ | |
map_contact.put(contact.Email,contact.Id); | |
} | |
for(Integer i=1;i<csvFileLines.size();i++){ | |
string[] csvRecordData = csvFileLines[i].split(','); | |
OpeationDirectoryInformation directory_record = new OpeationDirectoryInformation(); | |
if(map_account.get(csvRecordData[0])!= null){ | |
directory_record.accountNumber = map_account.get(csvRecordData[0]); //noestacion | |
} | |
else{ | |
directory_record.accountNumber = csvRecordData[0]; | |
} | |
directory_record.gpvFirstName = csvRecordData[1]; | |
directory_record.gpvLastName = csvRecordData[2]; | |
directory_record.gpvRadio = csvRecordData[3]; | |
directory_record.gpvPhone = csvRecordData[4]; | |
directory_record.gpvEmail = csvRecordData[5].replace('..','.'); | |
if(map_contact.get(csvRecordData[6])!=null){ | |
directory_record.ggEmail = map_contact.get(csvRecordData[6]); | |
} | |
else{ | |
directory_record.ggEmail = csvRecordData[6]; | |
} | |
directory.add(directory_record); | |
} | |
} | |
catch (Exception e) | |
{ | |
System.debug('### Error: '+e.getMessage()); | |
ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importin data Please make sure input csv file is correct'); | |
ApexPages.addMessage(errorMessage); | |
} | |
} | |
public PageReference updateDirectory(){ | |
List<Contact> contact_insert = new List<Contact>(); | |
for(OpeationDirectoryInformation operations:directory){ | |
Contact c = new Contact(); | |
c.FirstName = operations.gpvFirstName; | |
c.LastName = operations.gpvLastName; | |
c.Email = operations.gpvEmail; | |
c.ReportsToId = operations.ggEmail; | |
c.AccountId = operations.accountNumber; | |
contact_insert.add(c); | |
} | |
insert contact_insert; | |
return null; | |
} | |
public class OpeationDirectoryInformation{ | |
public String accountNumber{get;set;} | |
public String gpvFirstName{get;set;} | |
public String gpvLastName{get;set;} | |
public String gpvRadio{get;set;} | |
public String gpvPhone{get;set;} | |
public String gpvEmail{get;set;} | |
public String ggEmail{get;set;} | |
} | |
} | |
@isTest | |
private class TEST_Corpogas_ReadCSVController { | |
@isTest static void test_method_one() { | |
//preparing information in String | |
String csv_template = '81,firstname1,lastname1,52*40867*131,91180170,pedro.morales@corpogas.com.mx,rufina.ruiz@corpogas.com.mx\r91,firstname1,lastname1,52*40867*131,91180170,pedro.morales@corpogas.com.mx,rufina.ruiz@corpogas.com.mx'; | |
Blob blob_csv = Blob.valueOf(csv_template); | |
Corpogas_ReadCSVController csvController = new Corpogas_ReadCSVController(); | |
Corpogas_ReadCSVController.OpeationDirectoryInformation wrapper = new Corpogas_ReadCSVController.OpeationDirectoryInformation(); | |
csvController.csvFileBody = blob_csv; | |
List<Corpogas_ReadCSVController.OpeationDirectoryInformation> lis_wrapper = csvController.getDirectory(); | |
csvController.setDiretory(lis_wrapper); | |
csvController.importCSVFile(); | |
} | |
@isTest static void test_method_error() { | |
//preparing information in String | |
String csv_template = '123][[][][][]['; | |
Blob blob_csv = Blob.valueOf(csv_template); | |
Corpogas_ReadCSVController csvController = new Corpogas_ReadCSVController(); | |
Corpogas_ReadCSVController.OpeationDirectoryInformation wrapper = new Corpogas_ReadCSVController.OpeationDirectoryInformation(); | |
csvController.csvFileBody = blob_csv; | |
List<Corpogas_ReadCSVController.OpeationDirectoryInformation> lis_wrapper = csvController.getDirectory(); | |
csvController.setDiretory(lis_wrapper); | |
csvController.importCSVFile(); | |
PageReference re = csvController.updateDirectory(); | |
} | |
} | |
<apex:page controller="Corpogas_ReadCSVController"> | |
<apex:form > | |
<apex:pagemessages /> | |
<apex:pageBlock > | |
<apex:pageBlockSection columns="4"> | |
<apex:inputFile value="{!csvFileBody}" filename="{!csvAsString}"/> | |
<apex:commandButton value="Import Directory" action="{!importCSVFile}"/> | |
<apex:commandButton value="Update Directory" action="{!updateDirectory}"/> | |
</apex:pageBlockSection> | |
</apex:pageBlock> | |
<apex:pageBlock > | |
<apex:pageblocktable value="{!directory}" var="dir"> | |
<apex:column value="{!dir.accountNumber}" /> | |
<apex:column value="{!dir.gpvFirstName}" /> | |
<apex:column value="{!dir.gpvLastName}" /> | |
<apex:column value="{!dir.gpvRadio}" /> | |
<apex:column value="{!dir.gpvPhone}" /> | |
<apex:column value="{!dir.gpvEmail}" /> | |
<apex:column value="{!dir.ggEmail}" /> | |
</apex:pageblocktable> | |
</apex:pageBlock> | |
</apex:form> | |
</apex:page> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Doesn't work properly. Multiline cells are not handled correctly. You cannot just use
split
.