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?

  • 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 Feb 27 '18 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 Feb 27 '18 at 10:12
  • Do you mean you need to check only column headers, whether it is present in the file? – Hemant Jain Feb 27 '18 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. – Dilip Irabatti Feb 27 '18 at 11:17
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.

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

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.