You need to sign in to do that
Don't have an account?
Chris Dugal 4
I am querying for OpportunityLineItems and I need to put them in a map nested in a map were the inner map key is the product2Id and the outer map key is the opportunityId. But I cannot figure how to do this in the for loop. Also, the value of the inner map should be the OpportunityLineItem record. Thank you for your help.
how do I get data into a nested map?
List<OpportunityLineItem> oLIList = new List<OpportunityLineItem>(); oLIList = [SELECT Id, Product2Id, OpportunityId FROM OpportunityLineItem WHERE OpportunityId IN :oIdSet]; Map<Id, Map<Id, OpportunityLineItem>> oLIMap = new Map<Id, Map<Id, OpportunityLineItem>>(); for(OpportunityLineItem oLI : oLIList){
I am querying for OpportunityLineItems and I need to put them in a map nested in a map were the inner map key is the product2Id and the outer map key is the opportunityId. But I cannot figure how to do this in the for loop. Also, the value of the inner map should be the OpportunityLineItem record. Thank you for your help.
But base on your initial code and Assuming you have the duplicate rule, below would work.
List<OpportunityLineItem> oLIList = new List<OpportunityLineItem>();
oLIList = [SELECT Id, Product2Id, OpportunityId FROM OpportunityLineItem WHERE OpportunityId IN:oIdSet];
Map <Id,Map<Id,OpportunityLineItem>> oLIMap = new Map<Id,Map<Id,OpportunityLineItem>>();
for (OpportunityLineItem oLI : oLIList){
if(oLIMap.containsKey( oLI.OpportunityId))
oLIMap.get(oLI.OpportunityId).put(oLI.Product2Id,oLI);
else
oLIMap.put(oLI.OpportunityId, new Map<Id,OpportunityLineItem>{oLI.Product2Id=>oLI});
}
All Answers
List<OpportunityLineItem> oLIList = new List<OpportunityLineItem>();
oLIList = [SELECT Id, Product2Id, OpportunityId
FROM OpportunityLineItem
WHERE OpportunityId IN ('0066g000017SQAVAA4') ];
Map < Id, Map < Id, OpportunityLineItem > > oLIMap = new Map < Id, Map < Id, OpportunityLineItem > >();
for ( OpportunityLineItem oLI : oLIList ){
if ( !oLIMap.containsKey( oLI.OpportunityId ) )
oLIMap.put( oLI.OpportunityId, new Map < Id, OpportunityLineItem >() );
if ( !oLIMap.get( oLI.OpportunityId ).containsKey( oLI.Product2Id ) )
oLIMap.get( oLI.OpportunityId ).put( oLI.Product2Id, oLI );
}
system.debug( oLIMap );
--
Magulan Duraipandian
www.infallibletechie.com
But base on your initial code and Assuming you have the duplicate rule, below would work.
List<OpportunityLineItem> oLIList = new List<OpportunityLineItem>();
oLIList = [SELECT Id, Product2Id, OpportunityId FROM OpportunityLineItem WHERE OpportunityId IN:oIdSet];
Map <Id,Map<Id,OpportunityLineItem>> oLIMap = new Map<Id,Map<Id,OpportunityLineItem>>();
for (OpportunityLineItem oLI : oLIList){
if(oLIMap.containsKey( oLI.OpportunityId))
oLIMap.get(oLI.OpportunityId).put(oLI.Product2Id,oLI);
else
oLIMap.put(oLI.OpportunityId, new Map<Id,OpportunityLineItem>{oLI.Product2Id=>oLI});
}
Reference:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_maps.htm
MyFedLoan (https://www.myfedloan.one/)