Say I have a map:
Map<Lead, Map<Date, Decimal>>
How does Map navigation work in a scenario like this?
I would like to first search the Lead, and then search the Date, in order to ultimately find the Decimal value associated to that Lead.
Say I have a map:
Map<Lead, Map<Date, Decimal>>
How does Map navigation work in a scenario like this?
I would like to first search the Lead, and then search the Date, in order to ultimately find the Decimal value associated to that Lead.
There are two ways to get to it. If you know the exact keys of both maps you can get straight to it:
Decimal myDecimal;
if (myMap.containsKey(leadRecord))
{
myDecimal = myMap.get(leadRecord).get(dateInstance);
}
though this won't work nicely because your key is a lead sObject. Unless you have the same instance of the sObject you won't be able to match the key. I would rather use a lead Id, First Name or whatever combination of keys in order to make this work.
So instead of Map <Lead <Map <Date, Decimal>>
I would create Map <Id, Map <Date, Decimal>>
. Then it's guaranteed that you'll get the matching value based on the correct Id.
Id myLeadId = '00Q100000000ABC';
Date myDate = System.today();
Decimal myDecimal;
if (myMap.containsKey(myLeadId))
{
myDecimal = myMap.get(myLeadId).get(myDate);
}
Alternatively, you could iterate through the keys and perform checks for each entry. So in your current scenario you could do the following:
Id myLeadId = '00Q100000000ABC'; // This is the Id we're looking for
Date myDate = System.today(); // This is the Date we're looking for
Decimal myDecimal; // we'll store the matching value here (if there is one)
for (Lead leadRecord : myMap.keySet())
{
if (leadRecord.Id == myLeadId)
{
myDecimal = myMap.get(leadRecord.Id).get(myDate);
}
}
if (myDecimal == null)
{
// we did not find any matches
}
containsKey
.