I currently have a multi-demensional map/list object that is defined like this:
Map <Id,List <Map <Id,Custom_Inventory__c> >> complexMap = getComplexMapObject();
The object was difficult to create but it works perfectly fine, the first Id is AccountId, and the second index is ProductId.
Now I need a method that uses this object to verify if a record has a matching Account and Product Id within this collection. The first check is easy, something like this:
if(!complexMap.containsKey(line_item.Account__c)){
return FALSE;
}
The question I have is about the deeper index. I know I can loop through the map list within but wondering if I have to loop if I know what index I'm looking for. For example:
for (Map <Id,Custom_Inventory__c> inventoryMap : complexMap.get(variable.Account__c) ) {
// Iterate through inventoryMap, but I already know which
// index within this map I need... variable.Product__c)
if(inventoryMap.containsKey(rsLineItem.Product__c)){
return TRUE;
} else {
return FALSE;
}
}
If I can avoid the above loop I would like to. It will never be a very large loop but if I can avoid it I think its for the better.
List<Map>
instead of justMap
? If it weren't for that I would say your life would be a lot easier with complex keys instead of complex data structure.Map
key theProduct
id though? SoMap<Id, Map<Id, Custom_Inventory__c>>
seems sufficient. Or if you have multiple matches, useMap<Id, Map<Id, List<Custom_Inventory__c>>>
. Or better yetMap<DualIdKey, List<Custom_Inventory__c>>
.DualIdKey
idea was the perfect solution. In this instance I have 2 separate checks I need though, if the account is not part of the program then I need to flag it, and then if it is then I need to verify the products for that account are within the program. Originally I tried<Map Id, Map<Id, Custom_Inventory__c>>
but had trouble verifying if a product exist within a specific account. Not sure how I would do that.