0

Need some help with parsing csv thru apex . Below are the records i am trying to pass in csv but i am seeing an error : Ending position out of bounds: 255

1,,,"ABC,DEF",,"Robert, Thoma; (CFD)", ,Me USA; (AMB), ,,Background Voice (BI),0:00:18,,

2,,,"5,0,4",,"Brian, Smith; (RFD)",,Purple Blue green; (JGCF),, ,Background Voice (BI),0:00:18,,1

If i remove the second record i see no error , In the second record it seems to have trouble with these values "5,0,4" . If i swap the numbers with a string like record 1 it works fine , I cant see where my logic breaks , Below is the portion of code running behind this

for(Integer i=19;i<csvFileLines.size();i++){

                transient List<String> inputvalues = new List<String>();
                String[] values = new List<String>();
                 csvFileLines[i] = csvFileLines[i].replace('""',''); 
                 if(a_csvData.indexOf('"') == 0){
                    values = csvFileLines[i].split('","');  
                }else{

                    inputvalues = csvFileLines[i].split(',');
                    String str2 = '';
                    boolean b = false;

                    for(Integer j=0;j<inputvalues.size();j++){
                        String str1 = inputvalues[j];
                         if(str1.indexOf('"')==0) {
                            b=true;
                            str2 = '';
                            str1 = str1.subString(1);
                            str2 = str2 + str1;
                            if(str2.indexOf('"')>0) {
                                str2 = str2.subString(0,str2.indexOf('"'));
                                values.add(str2);
                                b=false;
                                }
                        }else if(str1.indexOf('"')!=0 && str1.indexOf('"')>1){
                            str1 = str1.subString(0,str1.length()-1);
                            str2 = str2 + ',' + str1;
                            if(str2.length()>255)
                                str2 = str2.subString(0,255);
                            values.add(str2);
                            str2='';
                            b=false; 
                        }else{
                            if(b==true) {
                                str2 = str2 + ',' + inputvalues[j];
                            }else{
                                if(inputvalues[j].length()>=255)
                                    inputvalues[j] = inputvalues[j].subString(0,255);
                                 values.add(inputvalues[j]);

                            }
                        }
                    }
                }

Can anyone please suggest where the problem lies , I would highly appreciate any help

Thanks Natasha

3
  • I don't think this is a specific Salesforce question, someone might be able to answer it but you may want to try this in Stackoverflow
    – EricSSH
    Commented Aug 2, 2017 at 18:45
  • I was just hoping to find out more on this error :Ending position out of bounds: 255 ,and what it meant in my case
    – Natasha
    Commented Aug 2, 2017 at 20:22
  • don't roll your own parser - see salesforce.stackexchange.com/a/4550/2602
    – cropredy
    Commented Aug 3, 2017 at 0:01

0

You must log in to answer this question.

Browse other questions tagged .