function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chris Dugal 4Chris Dugal 4 

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.
Best Answer chosen by Chris Dugal 4
sfdcBahusfdcBahu
Your map type would work only if you have a rule to prevent duplicate Opportunity Line Items. i.e one Product can exist only once under an Oppty.  Otherwise, your map should be Map<id,MAP<id,List<OpportunityLineitem>>>.

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

MagulanDuraipandianMagulanDuraipandian
Check the below code

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
sfdcBahusfdcBahu
Your map type would work only if you have a rule to prevent duplicate Opportunity Line Items. i.e one Product can exist only once under an Oppty.  Otherwise, your map should be Map<id,MAP<id,List<OpportunityLineitem>>>.

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});
        
}
This was selected as the best answer
Ruby SandraRuby Sandra
https://myfedloan.us/ Many thanks for that complete information!
Chris Dugal 4Chris Dugal 4
Thank sfdcBahu. Yes I am preventing dupes on the opp line items. Can you explain the "oLI.Product2Id=>oLI" part? I haven't seen the => in a put statement before.
sfdcBahusfdcBahu
@ chris Dugal, Glad it works for you. using = > is native way to initialize a map.  the format is key=> value;

Reference:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_maps.htm
Chris Dugal 4Chris Dugal 4
thanks for all your help!!
Alfred CollettAlfred Collett
Thank you. Your example didn't work for me but I managed to fix it, which was basically the same thing!
MyFedLoan (https://www.myfedloan.one/)
fed davidfed david
thanks for sharing. it is wonderful. MyFedLoan login (https://www.myfedloan.me/)
Merry PaulMerry Paul
This webpage is very professionally put together. I am relieved that I discovered this on Google. https://marykayintouch.cloud/marykayintouch-login/