2

i m importing a csv through apex but in a csv if a comma is in a cell, then the import will move everything after the comma to the next cell. Each comma shifts the data by one cell. i want that if their is a comma in a cell the whole word should remain in that i.e "a,c,,,b,," this whole word remain in a cell.....

this is my code plz help.....

public class importvalidate3 {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public string csvFileName;
public boolean validatefile=false;
public String[] csvFileLines{get;set;}
public String csvFileLines1;

public String[] csvFileLin{get;set;}
public List<account> acclist{get;set;}
public integer i;
public integer j=1;
public list<integer> i1;
public importvalidate3(){


    csvFileLines = new String[]{};
    i1 = new List<Integer>();
    }

public void importCSVFile(){
       try{
           //csvFileName = csvFileBody.filename();
           string[] csvRecordData;
           csvAsString = csvFileBody.toString();
          /* if(csvAsString.containsAny('""'))
           {
          //string b=a.substringAfter('"')+a.substringBefore('"');
            string b=csvAsString.substringBetween('"');
            system.debug('true b--->'+b);
            }*/
           system.debug('csvAsString--->'+csvAsString);
           csvFileLines = csvAsString.split('\n');

           system.debug('csvFileLines--->'+csvFileLines);

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

            system.debug('csvFileLines size is'+csvFileLines.size());
            system.debug('csvFileLines with i'+ csvFileLines[i]+i);

          /* if(csvFileLines[i].containsAny('""'))
           {
           system.debug('csvFileLines[i]--->'+csvFileLines[i]);
         //csvFileLines[i]=csvFileLines[i].substringBefore('"')+(csvFileLines[i].substringBetween('"')).trim()+csvFileLines[i].substringAfterLast('"');
          // csvFileLines[1]=(csvFileLines[i].substringBefore('"')).trim();
           //system.debug('hiiiiiiiiiiii-->'+csvFileLines[i].substringBefore('"'));
             csvFileLines[i]= csvFileLines[i].replaceAll('"','DBQT');
           system.debug(' csvFileLines[i]--->--'+ csvFileLines[i]);


           //csvFileLines[2]=csvFileLines[i].substringBetween('DBQT');
           //system.debug('csvFileLines[2]--->--'+ csvFileLines[2]);

           csvFileLines[3]=csvFileLines[i].substringAfter('DBQT');
           system.debug('csvFileLines[3]--->--'+ csvFileLines[3]);
           //system.debug('csvFileLines1--->'+csvFileLines[i].substringAfterLast('"'));
           system.debug('hiiiiiiiiiiii');


           }*/


            csvRecordData = csvFileLines1.split(',');
            system.debug('csvRecordData--->'+csvRecordData);


            string datatype= csvRecordData[0].trim();
            system.debug('datatype--->'+datatype);

            string fieldlabel=csvRecordData[1].trim();
            system.debug('fieldlabel--->'+fieldlabel);
            string length=csvRecordData[2].trim();
            system.debug('length--->'+length);

            string dec=(csvRecordData[3].trim());
            system.debug('dec--->'+dec);

            String req=csvRecordData[4].trim();
            system.debug('req--->'+req);

                string uni=csvRecordData[5].trim();
                string ext=csvRecordData[6].trim();
                Matcher isnumbers = Pattern.Compile('^[0-9]+$').matcher(length);
                Matcher decimalisnumber = Pattern.Compile('^[0-9]+$').matcher(dec);
                system.debug('### ' + datatype);
                i1.clear();
                if(datatype!='')
                {
                    if(datatype=='text')
                    {
                        if(!((string.isnotblank(fieldlabel))&&(string.isBlank(dec)&& string.isnotblank(length)&&(isnumbers.matches()))&&((req.equalsIgnoreCase('TRUE')||req.equalsIgnoreCase('FALSE')||req.equalsIgnoreCase('0')||req.equalsIgnoreCase('1'))&&(uni.equalsIgnoreCase('TRUE')||uni.equalsIgnoreCase('FALSE')||uni.equalsIgnoreCase('0')||uni.equalsIgnoreCase('1'))&&(ext.equalsIgnoreCase('TRUE')||ext.equalsIgnoreCase('FALSE')||ext.equalsIgnoreCase('0')||ext.equalsIgnoreCase('1')))))
                        {
                        i1.add(i);
                        system.debug('text datatype is internally wrong'+i1);
                        }
                    }
                    else if(datatype=='Number')
                    {
                        system.debug('this is number datatype');
                        if(!((string.isnotblank(fieldlabel))&&(((string.isnotBlank(dec))&&(decimalisnumber.matches()))&&((string.isnotblank(length))&&(isnumbers.matches())))&&(req.equalsIgnoreCase('TRUE')||req.equalsIgnoreCase('FALSE')||req.equalsIgnoreCase('0')||req.equalsIgnoreCase('1'))&&(uni.equalsIgnoreCase('TRUE')||uni.equalsIgnoreCase('FALSE')||uni.equalsIgnoreCase('0')||uni.equalsIgnoreCase('1'))&&(ext.equalsIgnoreCase('TRUE')||ext.equalsIgnoreCase('FALSE')||ext.equalsIgnoreCase('0')||ext.equalsIgnoreCase('1'))))
                        {
                        i1.add(i);
                        system.debug('number datatype is internally wrong'+i1);
                        }

                    }
                    else
                    {
                       i1.add(i);
                       system.debug('datatype is wrong'+i1);
                    }

                }
            else
            { 
            i1.add(i);
            system.debug('fill datatype'+i1);
            }   
        }


    }
    catch(exception e)
    {
    system.debug(e);
    }
}}

2 Answers 2

1

When using csv (comma separated values) files, be certain to use double quote to enclose each field.

For example:

This csv file has 4 columns, "LastName", "FirstName", "Title", "HomePhone"

This will load correctly into Contact Object:

"Smith, SR.","Joe","Chief Cook","555-555-1212"

This file will NOT load correctly into Contact Object:

Smith, SR.,Joe,Chief Cook,555-555-1212

1
  • Welcome to SFSE, good first answer!
    – Matt Lacey
    May 21, 2014 at 22:56
1

You may be better off starting from this Parse a CSV with APEX code sample. It avoids the expense of character by character processing by starting with some replaceAll calls so that embedded commas can be handled appropriately. And has presumably been tested by a number of people.

You can add your own logic as required, or perhaps better take the results from that code and apply your logic in a second pass over the data.

You must log in to answer this question.

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