1

CSV File contains headers. Full Name - is first column header, Manager Email - last column header.

after file input, i create map that contains rowIndex (for VF apex:repeat), 'Header'='Value' (Map)

If I try to display Full Name (first header) or Manager Email (last header) on VF-page:

Map key Full Name not found in map

Error is in expression '{!values[n]['Full Name']}' in component in page ruit_ios_app

But if i delete this expression from page, it works.

APEX CONTROLLER

public void importCSVFile() {
        try {
                csvAsString = csvFileBody.toString();
                csvFileLines = csvAsString.split('\n');
                List<String> headers = csvFileLines[0].split(';');

                for (Integer i = 1; i < csvFileLines.size(); i++) {
                    List<String> csvRecordData = csvFileLines[i].split(';');

                    Map<String, String> newMap = new Map<String, String>();
                    for (Integer j = 0; j < headers.size(); j++) {
                        newMap.put(headers[j], csvRecordData[j]);
                    }
                    numbers.add(i);
                    values.put(i, newMap);
                }

PAGE

 <apex:repeat value="{!numbers}" var="n">
                                <tr>
                                    <td>{!values[n]['Full Name']}</td>
                                    <td>{!values[n]['Email']}</td>
                                    <td>{!values[n]['Last Check In']}</td>
                                    <td>{!values[n]['OS Version']}</td>
                                    <td>{!values[n]['Device Model']}</td>
                                    <td>{!values[n]['Country']}</td>
                                    <!--<td>{!values[n]['Manager Email']}</td>-->
                                </tr>
                            </apex:repeat>
  • You should not assume any specific ordering in the keyset. Sets have no ordering. – Phil W Sep 2 at 14:17
  • hi Phil! Which ordering do you mean? do you mean when i said "first column"? – Ivan S. Sep 2 at 14:24
  • You edited the question and removed the assumption you could get the 0th entry from the keyset as a list. This was what I was talking about at the time – Phil W Sep 2 at 15:26
  • a bit unusual for csv columns to be delimited by semi-colon rather than comma – cropredy Sep 3 at 1:26
  • it's csv generated from excel, i don't know why it generates like that)) – Ivan S. Sep 3 at 6:33
0

That it is only occurring for a subset of the columns makes me think that your input CSV might not contain the exact headers Full Name and Manager Email.

It is possible that there are encoding issues with those headers. E.g. In reality they might actually be "Full Name", or Full Name (with a trailing space), or any other combination of characters that would be possible in a CSV. Especially if it was encoded poorly.

At a minimum, I'd suggest starting with assertions to ensure the expected headers are present.

List<String> headers = csvFileLines[0].split(';');
System.assert(headers.contains('Full Name'));
System.assert(headers.contains('Email'));
System.assert(headers.contains('Manager Email'));

In reality you would do better error handling here when the input CSV doesn't meet the expected format, but it will do to at least get things going.


You could also assert that the expected values are in the resulting map.

Map<String, String> newMap = new Map<String, String>();
for (Integer j = 0; j < headers.size(); j++) {
    newMap.put(headers[j], csvRecordData[j]);
}
System.assert(newMap.containsKey('Full Name'));
System.assertNotEquals(null, newMap['Full Name']);
// Repeat assertions for other problem keys

numbers.add(i);
//...
  • Hello Daniel! Thank you really for you reply. But the reason is these headers are first and last. If i move Full Name column to 2nd position and manager Email to 2nd from the end - i can display them on vf page. – Ivan S. Sep 3 at 6:33
  • @IvanS. In that case I'd expand that assertions being made in the code to include the records going into newMap. The objective is to narrow down where the problem is occurring. – Daniel Ballinger Sep 3 at 19:13

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.