Search code examples
javaseleniumsalesforceopencsv

Java Selenium - CSV UTF-8 format file creation is not working for Salesforce


I am facing a unique challenge. My application is based on SalesForce, where I have to create a list (First Name, Last Name, Email) and upload in the application. I am using OpenCSV to generate the file and uploading but application is not recognizing.

Manually How it works:

  1. Download template provided by developers which is .xls unicode format
  2. Open with Microsoft Excel
  3. Populate data and save as CSV UTF-8 (Comma delimited)(*.csv)

It works perfectly fine while modifying current template manually.

Automation Approach:

  1. With help of Java and OpenCSV copying the file to use the same template
  2. Populate data
  3. Flush file
  4. Copy file to be in CSV format

But when I upload the file, application is not accepting. Not getting any error message as well.

Please find below code that I have currently. Any help will be appreciated.

    public void prepareCSVfile(int numberOfRecords) throws IOException {

    String filePath = Constants.CONFIGURATION_PATH + "csv\\" + Constants.RUNNAME + ".xls";
    String fileName = Constants.CONFIGURATION_PATH + "csv\\" + Constants.RUNNAME + ".csv";
    File source=new File(Constants.CONFIGURATION_PATH + "csv\\" +"Template.xls");
    File destination=new File(filePath);

    copyFile(source,destination);
    
    CSVWriter writer = new CSVWriter(new OutputStreamWriter(new FileOutputStream(filePath, true), "UTF-8"), CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER,CSVWriter.DEFAULT_LINE_END);

    for (int i = 0; i < numberOfRecords; i++) {
        String firstName = Commons.getRandomAlphabet(4).toUpperCase();
        String lastName = Commons.getRandomAlphabet(4).toUpperCase();
        String emailAddress = Commons.getRandomAlphabet(8).toUpperCase() + "@email.com";

        String lines[] = { firstName, lastName, emailAddress };
        writer.writeNext(lines);
        System.out.println(lines);
    }

    // Flushing data from writer to file
    writer.flush();
    writer.close();
    copyFile(new File(filePath),new File(fileName));
    System.out.println("Data entered");
}

Solution

  • Just wanted to let the community know that I have found a solution. While writing the file openCSV is not a good option. I am using Apache commons csv and it's working fine.

    Here is updated working code.

    String filePath = Constants.CONFIGURATION_PATH + "csv\\" + Constants.RUNNAME + ".csv";
        File source=new File(Constants.CONFIGURATION_PATH + "csv\\" +"MyTemplate.csv");
        
        CSVPrinter writer = new CSVPrinter(new FileWriter(filePath), CSVFormat.EXCEL);
        writer.printRecord("First Name","Last Name","Email");
        for (int i = 0; i < numberOfRecords; i++) {
            
            List<String> nextLine = new ArrayList<>();
              nextLine.add(Commons.getRandomAlphabet(6).toUpperCase());
              nextLine.add(Commons.getRandomAlphabet(6).toUpperCase());
              nextLine.add(Commons.getRandomAlphanumeric(10).toUpperCase() + "@email.com");
            
              writer.printRecord(nextLine);
            //data.add(new String[]{ firstName, lastName, emailAddress });
        }
        
        writer.flush();
        writer.close();