+ Start a Discussion

Hi,Can anyone help me for this Apex class,I am reading CSV file using Apex class,the Value is not updating in object

The Transaction_Amount__c is not updating in the Investor_Banking_Info_Entries__c object,iam retrieving the value in Amount variable,Can anyone please tell how to assign this varible to Transaction_Amount__c field

public class importDataFromCSVController {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public String[] csvFileLines{get;set;}
public string Amount;

public List<Investor_Banking_Info_Entries__c> acclist{get;set;}
  public importDataFromCSVController(){
    csvFileLines = new String[]{};
    acclist = New List<Investor_Banking_Info_Entries__c>();
  public void importCSVFile(){
           csvAsString = csvFileBody.toString();
           csvFileLines = csvAsString.split('\n');
           for(Integer i=1;i<csvFileLines.size();i++){
               Investor_Banking_Info_Entries__c accObj = new Investor_Banking_Info_Entries__c() ;
               string[] csvRecordData = csvFileLines[i].split(',');
               accObj.Investor_Name__c = csvRecordData[0] ;
               accObj.Account_Number__c  = csvRecordData[1];  
               Amount  = csvRecordData[2];
              System.debug('The value isSSSSSSSSSSSSSSSSSSSSSSSSSS: ' +Amount );
              Amount = string.valueof(accObj.Transaction_Amount__c);
               System.debug('The value is: ' +Amount );
        insert acclist;
        catch (Exception e)
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importin data Please make sure input csv file is correct');
Amritesh SahuAmritesh Sahu

Please use this method to extract the CSV content in List<List<String>>
private List<List<String>> parseCSV(String contents,Boolean skipHeaders) 
            List<List<String>> allFields = new List<List<String>>();    
            // replace instances where a double quote begins a field containing a comma    
            // in this case you get a double quote followed by a doubled double quote    
            // do this for beginning and end of a field    
            contents = contents.replaceAll(',"""',',"DBLQT').replaceall('""",','DBLQT",');    
            // now replace all remaining double quotes - we do this so that we can reconstruct    
            // fields with commas inside assuming they begin and end with a double quote    
            contents = contents.replaceAll('""','DBLQT');    
            // we are not attempting to handle fields with a newline inside of them    
            // so, split on newline to get the spreadsheet rows    
            List<String> lines = new List<String>();    
            try {        
                lines = contents.split('\n');    
            catch (System.ListException e) {        
                System.debug('Limits exceeded?' + e.getMessage());    
            Integer num = 0;    
            for(String line : lines) {        
                // check for blank CSV lines (only commas)        
                if (line.replaceAll(',','').trim().length() == 0) break;
                List<String> fields = line.split(',');          
                List<String> cleanFields = new List<String>();        
                String compositeField;        
                Boolean makeCompositeField = false;        
                for(String field : fields) {            
                    if (field.length() > 1 && field.startsWith('"') && field.endsWith('"'))
                        else if (field.startsWith('"')) {                
                            makeCompositeField = true;                
                            compositeField = field;            
                        else if (field.endsWith('"')) {
                            compositeField += ',' + field;                
                            makeCompositeField = false;            
                        else if (makeCompositeField) {                
                            compositeField +=  ',' + field;            
                        else {
                if (skipHeaders) allFields.remove(0);    
                return allFields;       

You can extract the information using :
Blob csvFile; // Get blob csvFile value from VF inputFile

List<List<String>> csvContent = parseCSV(csvFile.toString(),false);
for(Integer i=0; i<csvContent.size(); i++)
	String[] csvRow = csvContent[i];
	for(Integer j=0;j<csvRow.size();j++)
		String cellValue = csvRow[j];

Hope it helps !!

Thanks Amritesh

This is totally new for me,Can you please explain this more or update in my code please