0

I've implemented an apex algorithm to upload a CSV file into Salesforce. In the CSV file there are 7 necessary columns and I keep all of them in a list of strings:

private List<String> necessaryColumns = new `List<String>` {'column01' , 'column02', 'column03', 'column04', 'column05', 'column06', 'column07'};

I need to validate by column names.

I do not need to check the amount of columns.

If one of the 7 columns is missing in the CSV file, then I need to show a warning message to the users.

Please advise how to validate by column name?

4
  • Idea is quite straightforward -- you go and split your csv by rows, and then by columns, and after that check amount of columns. If those does not match your expectation -- show a warning. And in case if you hit any issues during implementation of this -- you can share it here
    – kurunve
    Commented Feb 27, 2018 at 10:03
  • @kurunve I need to validate by column names. I do not need to check the amount of columns. If one of the 7 column name is missing in the CSV file, then I need to show a warning message. Pleas advise how to validate by column name?
    – lemdev
    Commented Feb 27, 2018 at 10:12
  • Do you mean you need to check only column headers, whether it is present in the file? Commented Feb 27, 2018 at 10:25
  • I think you can have a wizard for your csv file process. First page will be file upload. Second will be mapping. In this you can show your csv file header field mapping with salesforce object fields. Here you can validate your required field mapping. Commented Feb 27, 2018 at 11:17

1 Answer 1

0

You can do this using set intersection:

Set<String> columnsInFile = new Set<String>(LIST_OF_COLUMNS);

if (!columnsInFile.containsAll(necessaryColumns)) {
    // Do error handling here.
}

containsAll returns false if the given Set does not contain all of the items in the List passed.

1
  • thank you so much. That's exactly what I've had in mind.
    – lemdev
    Commented Feb 27, 2018 at 14:00

You must log in to answer this question.

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