You need to sign in to do that
Don't have an account?
sfadm sfadm
CSV file upload validation
In the following piece of code:
What I'm trying to achieve is to show the message only if one of the columns in the CSV file is missing.
If there are 20 columns in the CSV file but for instance columnA is missing in the file only then the ApexPages.Message has to be displayed for the users.
Please advise how this can be achieved with apex?
List<String> necessaryColumns = new List<String> {'columnA', 'columnB', 'columnC', 'columnD', 'columnE', 'columnF','columnG'}; Set<String> columnsInFile = new Set<String>(necessaryColumns); System.debug('columnsInFile' + columnsInFile); // Get all column names from the CSV file List<String> columnNames = csvRecords.get(0); for(String columnName :columnNames) { System.debug('columnName: ' + columnName); if(!columnsInFile.contains(columnName)) { System.debug('clmnName: ' + columnName); ApexPages.Message myMessage = new ApexPages.Message(ApexPages.Severity.FATAL, 'The uploaded file is invalid'); ApexPages.addMessage(myMessage); return; } }The issue I have is that the names of the columns in the set are in strict order.
|DEBUG|columnsInFile{columnA, columnB, columnC, columnD, columnE, columnF, columnG}and if the order of the columns in the CSV file is changed for instance:
columnG, columnF, columnD, columnC, columnA, columnBThe algorithm is entering the if block and is showing the message, which is totally wrong.
What I'm trying to achieve is to show the message only if one of the columns in the CSV file is missing.
If there are 20 columns in the CSV file but for instance columnA is missing in the file only then the ApexPages.Message has to be displayed for the users.
Please advise how this can be achieved with apex?
All Answers
@Dev_Arya, as requested here is the output for line 07:
However the output for line 09:
If I manually change the order of the columns in the following order: The output for line 09 is: Please advise and appropriate solution to this issue.Appologies, i could not look in to it, got stuck with our own org issues. :-P
Could you figure this out so far, else we can look in to it? As a matter of curiosity, I would really like to know the use case.
Cheers,
Dev