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>
5
  • You should not assume any specific ordering in the keyset. Sets have no ordering.
    – Phil W
    Commented Sep 2, 2019 at 14:17
  • hi Phil! Which ordering do you mean? do you mean when i said "first column"?
    – Ivan S.
    Commented Sep 2, 2019 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
    Commented Sep 2, 2019 at 15:26
  • a bit unusual for csv columns to be delimited by semi-colon rather than comma
    – cropredy
    Commented Sep 3, 2019 at 1:26
  • it's csv generated from excel, i don't know why it generates like that))
    – Ivan S.
    Commented Sep 3, 2019 at 6:33

1 Answer 1

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);
//...
2
  • 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.
    Commented Sep 3, 2019 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. Commented Sep 3, 2019 at 19:13

You must log in to answer this question.

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