Newer Version Available

This content describes an older version of this product. View Latest

Referencing Apex Maps and Lists

Visualforce pages that use dynamic bindings can reference the Apex Map and List data types in their markup.

For example, if an Apex List is defined as follows:
It can be accessed in a Visualforce page like this:
Similarly, if you have the following Apex Map:
Your Visualforce page can show the values like this:

Use dynamic references to lists and maps in an <apex:inputText> tag to create forms using data that isn’t in your organization’s custom objects. Working with a single map can be much simpler than creating a series of instance variables in an Apex controller or creating a custom object just for the form data.

Here’s a Visualforce page that uses a map to hold form data for processing by a custom controller:
And here’s a simple controller that works with the form:
A Map can contain references to sObjects or sObject fields. To update those items, reference a field name in the input field:

Unresolved Dynamic References

Keep in mind the following issues that can arise at run time if a dynamic reference doesn’t resolve:
  • If there isn’t a value mapped to a particular key, the Visualforce page returns an error message. For example, with this controller:
    This page causes an error at run time:
  • If the key is null, the Visualforce page renders an empty string. For example, using the same controller as above, this page shows an empty space: