Viewing Salesforce Collections in Excel: Maps

The last couple of weeks we have taken a look at both Sets and Lists in Excel, today we will look at Maps the third and final collection. A Map is a collection of key-value pairs, where the key is unique and both the key and value can be of any data type. Typically, the Salesforce Id is used as the key with the record itself as its value. If we were going to create a map of our contacts like our report used to generate our Excel file, column B would be the key and the entire row would be its matching value.ContactsExport

To walk through an example, let’s log into our Development Org and locate Tim Barr’s record. We can easily find Tim’s Contact Id in the Salesforce URL, it will look something like this Keep in mind that your org may have a different instance as well as a different Contact Id, my particular instance is na24 and for my Tim Barr record the Id is 0031a000003wRFU. Let’s go back into Excel and do an Advanced Search using the Id as the criteria and making sure to check the Match case. The row that the Id is found on is the value associated with the key (Id) in this case.

Screenshot 2015-10-17 22.28.03

Now let’s see how this would look in code.

1. Log into your Developer Org
2. Launch the Developer Console
3. Select Open Execute Anonymous Window from the Debug Menu
4. Enter the following code snippet:

5. Press the Execute button (Verify the Open Log option is checked)
Note: You may need to set your user to be monitored in the Debug Logs for the Logs to appear. Results may vary depending on the records in your Developer Org.
The resulting Debug:
Screenshot 2015-10-17 23.09.13
Looking for more about Maps or collections in general? Head on over to the Apex Code Developer Guide.

Leave a comment

Your email address will not be published. Required fields are marked *