0

I need to read header column of csv file and those fileds i have to display in vf page. And those filed i shuld be able to map with the salesforce object . After mapping, insert/update the csv record data into object. We can get the fields of sobjects by the schema.descrime method. But I am not getting idea to map the csv fields to the sobject fields. Please someone help me in getting some idea how to get the header columns from the excel and insert into objects. This is the code I have used for this

public class uploadCSVcontroller {

    public Blob contentFile { get; set; }
    public String nameFile { get; set; }
    public Integer rowCount { get; set; }
    public Integer colCount { get; set; }

    public List<List<String>> getResults() {
        List<List<String>> parsedCSV = new List<List<String>>();
        rowCount = 0;
        colCount = 0;
        if (contentFile != null){
            String fileString = contentFile.toString();

            parsedCSV = parseCSV(fileString, true);
            // system.debug('##########'+parsedCSV);
            rowCount = parsedCSV.size();
        }
        return parsedCSV;
    }

    public static List<List<String>> parseCSV(String contents) {
        List<List<String>> allFields = new List<List<String>>();

        List<String> lines = new List<String>();
        try {

            lines = contents.split('/'); 
            system.debug('#########'+lines);
        } catch (System.ListException e) {
            System.debug('Limits exceeded?' + e.getMessage());
        }
        for(String line: lines) {
            // check for blank CSV lines (only commas)
            List<String> cleanFields = new List<String>();
            List<String> fields = line.split(',');  

            for(String field: fields){
                if(field.startsWith('') && field.endsWith('') ){
                    cleanfields.add(field);
                }
            }
            allFields.add(cleanFields);  
        }
        if (skipRecords)
        allFields.add(0);
        return allFields;       
    }

}
  • What you have tried so far, are you stuck somewhere? Please paste the code here so people can help you. – rahul gawale Apr 30 at 11:09
  • 1
    @rahulgawale, I have updated my question now – Sachin Apr 30 at 11:35
  • I used add in the last if statement and it is showing error as "Method does not exist or incorrect signature: void add(Integer) from the type List<List<String>>". Can anybody tell whether there is option to add by using some keyword. If I use that then only the first row will come with the all the fields in the csv. – Sachin Apr 30 at 12:44
  • addfields can accept List<String> only. – rahul gawale May 1 at 9:31
  • this should work allFields.add(fields); – rahul gawale May 1 at 9:56
0

Act as below:

  1. Split the CSV string by ’/n’ into a list of lines.

  2. The first element includes the headers, split them by comma into a list of headers. By far you have list of lines (A) and list of headers (B)

  3. Loop through list A, for each line:

    • Split the line by a comma. List of values (C)
    • Loop through list B.
    • For each header, push the header from list B and its value from list C as they have the same index into map where the key is the header from list B and the value is the value from list C.
    • after the loop, insert it into list of maps.

      1. Create a wrapper class in Apex which includes all the headers from list B.

      2. serialize the List of maps and the wrapper class into a list of wrapper class objects.

      3. You have a list which each element includes the data from each line.

0

Thanks for your help. I used below class to complete that vf Page. Posting that if someone needs in future.

public class uploadCSVcontroller {

public Blob contentFile { get; set; }
public String nameFile { get; set; }

public List<SelectOption> getResults() {
    List<SelectOption> parsedCSV = new List<SelectOption>();
    if (contentFile != null){
        String fileString = contentFile.toString();
        parsedCSV = parseCSV(fileString);  
    }
    return parsedCSV;
}

public static List<SelectOption> parseCSV(String contents) {
    List<SelectOption> allFields = new List<SelectOption>();
    allFields.Clear();
    List<String> fields = new List<String>();
    fields = contents.split('\n');
    String headers= fields[0];

      List<String> field =  headers.split(',');
     allFields.add(new SelectOption('--Select a Field--','--Select a Field--'));
    for(String field1: field){
        allFields.add(new SelectOption(field1, field1));
        system.debug('#######'+allFields);

    }
    return allFields;       
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.