0

Here is my Controller function:

Public PageReference readcsvFile(){
    try{
        fieldList = New List<string>();
        assignmentList = new List<AssignmentWrapper>();
        isErrorInProcessing = false;
        PageNumber = 1;
        totalPages = 0;
        totalRecord = 0;
        processedRecords=0;
        pageSize = 10;
        pageSizeMultiplier = 1;
        recordLimit = 100;
        remainingRecords=0;

        PageNumberErrorRecord = 1;
        totalPagesErrorRecord = 0;
        totalRecordErrorRecord = 0;
        pageSizeErrorRecord = 10;
        pageSizeMultiplierErrorRecord = 100;
        recordLimitErrorRecord = 1;
        isCsvHasRecord= false;

        hideInputField= false;
        isProcessingStart  =false;

        ApexPages.Message errorMessage;   
        HttpRequest tmpRequest = new HttpRequest();
        tmpRequest.setBodyAsBlob(csvFileBody);
        CSVStringFormat = tmpRequest.getBody();
        if(CSVStringFormat.trim() != null && CSVStringFormat.trim() != ''){
            List<String> lineData = CSVStringFormat.split('\n');
            List<String> headerData = lineData[3].split(',');
            for(Integer i = 5; i < headerData.size(); i++){
                fieldList.add(headerData[i].remove('"'));
            }

            for(Integer i = 4; i < lineData.size(); i++){
                AssignmentWrapper aw = new AssignmentWrapper();
                List<String> rowData = lineData[i].split(',');
                aw.projectName = rowData[0].remove('"');
                aw.projectId = rowData[1].remove('"');
                aw.assignmentName = rowData[2].remove('"');
                aw.assignmentId = rowData[3].remove('"');
                aw.resourceName = rowData[4].remove('"');
                aw.WeekMonthHeader = new List<Decimal>();
                for(Integer j = 5; j < rowData.size(); j++){
                     System.debug(rowData[j]);
                    if(rowData[j] != null && rowData[j] != ''){
                        String t = rowData[j];
                        //System.assertEquals(true, t.isNumericSpace());
                        aw.WeekMonthHeader.add((Decimal)(Integer.valueOf(t)));

                    }else{
                        aw.WeekMonthHeader.add(0.0);
                    }
                }
                assignmentList.add(aw);
            }

            if(assignmentList.size()>1000){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'The import process is limited to 1,000 records per batch. If you have more than 1,000 changes to upload, please divide the data into multiple files and upload each file separately.'));                    
                isCsvHasRecord= false;                
            } else if (assignmentList.size()>0){
                isCsvHasRecord=true;
            }

            if(isCsvHasRecord)
            {
                remainingRecords= assignmentList.size();
                PageNumber = 1;
                totalRecord = assignmentList.size();
                totalPages = totalRecord / pageSize;
                if(Math.mod(totalRecord,pageSize) != 0 ){
                    totalPages++;
                }
                reCalculateWrapperList();
            }
        } else {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,'Please select a csv file.'));         
        }
    }catch(Exception ex){
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error, ex +' Please select a valid csv file.'));  
    }
    return null;
}

CSV File:

"Project name","Project ID","Assignment","Assignment ID","Resource name","May-2017","Jun-2017","Jul-2017","Aug-2017"
"test","066390000009Ef4AAE","test Assignment","066390000009Ef4AAE","resource 1",10,20,20,9
"test","066390000009Ef4AAE","test Assignment 1 ","066390000009Ef4AAE","resource 2",30,40,50,4
"test","066390000009Ef4AAE","test Assignment 2","066390000009Ef4AAE","resource 3",,,10,10
"test2","066390000009Ef4AAE","test1 Assignment 2","066390000009Ef4AAE","resource 4",2,,,

Error that I am facing: (Only for last value just before '\n')

FATAL_ERROR|System.TypeException: Invalid integer: 9

Why I am facing this issue ? What is the solution to read this file ?

Thanks!

1 Answer 1

2

I suspect that there's a hidden \r in your CSV (common if you're using Windows to make your CSV files), so you might want to trim the value first:

aw.WeekMonthHeader.add((Decimal)(Integer.valueOf(t.trim())));

Also, trim() never returns null and will cause an exception if you call it on a null string. Consider altering your code:

if(String.isNotBlank(CSVStringFormat)){

String.isNotBlank returns true only when the string is not null, empty, and does not contain only white space.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .