0 votes
1 view
in Salesforce by (11.9k points)

I have a map object which stores <Id, String> where the Id is a contact Id, and the String is a generated email message.

I have successfully looped through the map and have been able to pull out the values (The String portion) as I iterate through the map.

What I would like to be able to do is also grab the key when I grab the value. This is very simple to do in most languages, but I can't seem to find out how to do it in apex.

This is what I have right now:

Map<Id,String> mailContainer = new Map<Id,String>{};

for(String message : mailContainer.values())

{

    // This will return my message as desired

    System.debug(message);

}

What I would like is something like this:

for(String key=>message : mailContainer.values())

{

    // This will return the contact Id

    System.debug(key);

    // This will return the message

    System.debug(message);

}

Thanks in advance!

1 Answer

0 votes
by (31.6k points)

Try using the following code to iterate over the keys instead of values.

for (Id id : mailContainer.keySet())

{

    System.debug(id);

    System.debug(mailContainer.get(id));

}

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...