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
Kenneth KimbrellKenneth Kimbrell 

How to group data with nested maps

I am working on trying to group my Event data with maps. What I am trying to accomplish is to group my Events in weeks > bookers > roll up count for booked appt for each week. My logic so far is:

map<string,list<Event>> Date2Event = new map<string,list<Event>>();
for(Event e: eventLst){
    if(!Date2Event.containsKey(dateMapKey(e.CreatedDate))){
         Date2Event.put(dateMapKey(e.CreatedDate), new list<Event>());
     }
     Date2Event.get(dateMapKey(e.CreatedDate)).add(e);
}

public string dateMapKey(datetime dt){
    	datetime myDt = dt;
    	date myDate = myDt.date();
    	date StartOfWeekDate = myDate.toStartOfWeek();
    	string mapKey = StartOfWeekDate.format() + '-' + StartOfWeekDate.addDays(6).format();
    	return mapKey;
}

So I have a function that is basically setting the map's key with a string of start date to end date. So far what I have accomplished is to group the events by week. Now I need to nest the grouping more and group by week and by bookers, then roll up count for each booked appt for that week. Any ideas would be greatly appreciated. Thanks.
Best Answer chosen by Kenneth Kimbrell
Kenneth KimbrellKenneth Kimbrell

after some time, I finally decided the best design patter for the nested mapping. I will post my answer here for others, since the responses I get are sometimes too generalized than what is needed.

        map<string,map<string,map<string,decimal>>> mapData = new map<string,map<string,map<string,decimal>>>();

        //get all events
        for(Event e: eventLst){
        	string dateKey = dateMapKey(e.CreatedDate);
        	map<string,map<string,decimal>> bmap = new map<string,map<string,decimal>>();
        	map<string,decimal> fieldMap = new map<string,decimal>();
        	if(mapData.get(dateKey) != null){
        		bmap = mapData.get(datekey);
        		if(bmap.get(e.Booker__c) != null){
        			fieldMap = bmap.get(e.Booker__c);
        		}
        	}
        	decimal countShow=0,countNoShow=0,showPerc=0;
            if(fieldMap.get('countShow') != null){
            	countShow = fieldMap.get('countShow');
            }
        	if(fieldMap.get('countNoShow') != null){
        		countNoShow = fieldMap.get('countNoShow');
            }
        	if(fieldMap.get('countNoShow') != null && fieldMap.get('countShow') != null){
        		showPerc = fieldMap.get('showPerc');
        	}
        	if(e.Booking_Status__c == 'Show'){
        		countShow++;
        	}
        	if(e.Booking_Status__c == 'No Show'){
        		countNoShow++;
        	}
        	showPerc = countNoShow != 0 ? (countShow / (countShow + countNoShow)* 100).setScale(1) : 0.0;
        	map<string,decimal> fm = new map<string,decimal>();
            fm.put('countShow',countShow);
        	fm.put('countNoShow',countNoShow);
        	fm.put('showPerc',showPerc);
        	map<string,map<string,decimal>> bm = new map<string,map<string,decimal>>();
        	bm.put(e.Booker__c,fm);
        	mapData.put(dateKey,bm);
		}

All Answers

Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Kenneth

Try using this structure of map:
Map<String,Map<String,List<Event>>

This would be map of weekday > bookers Name or if you have it as a record then use Id > total rollup of the booker for that week.

Cheers!!!!
Luv vinLuv vin
You can go here to find out more:
(https://ringtonesmobile.net (https://ringtonesmobile.net/)) - (https://ringtonesmobile.net/bollywood)
Kenneth KimbrellKenneth Kimbrell
Syed Insha Jawaid 2 -

Can you show me an example of how I might use a nested map while looping through the events?
Kenneth KimbrellKenneth Kimbrell

after some time, I finally decided the best design patter for the nested mapping. I will post my answer here for others, since the responses I get are sometimes too generalized than what is needed.

        map<string,map<string,map<string,decimal>>> mapData = new map<string,map<string,map<string,decimal>>>();

        //get all events
        for(Event e: eventLst){
        	string dateKey = dateMapKey(e.CreatedDate);
        	map<string,map<string,decimal>> bmap = new map<string,map<string,decimal>>();
        	map<string,decimal> fieldMap = new map<string,decimal>();
        	if(mapData.get(dateKey) != null){
        		bmap = mapData.get(datekey);
        		if(bmap.get(e.Booker__c) != null){
        			fieldMap = bmap.get(e.Booker__c);
        		}
        	}
        	decimal countShow=0,countNoShow=0,showPerc=0;
            if(fieldMap.get('countShow') != null){
            	countShow = fieldMap.get('countShow');
            }
        	if(fieldMap.get('countNoShow') != null){
        		countNoShow = fieldMap.get('countNoShow');
            }
        	if(fieldMap.get('countNoShow') != null && fieldMap.get('countShow') != null){
        		showPerc = fieldMap.get('showPerc');
        	}
        	if(e.Booking_Status__c == 'Show'){
        		countShow++;
        	}
        	if(e.Booking_Status__c == 'No Show'){
        		countNoShow++;
        	}
        	showPerc = countNoShow != 0 ? (countShow / (countShow + countNoShow)* 100).setScale(1) : 0.0;
        	map<string,decimal> fm = new map<string,decimal>();
            fm.put('countShow',countShow);
        	fm.put('countNoShow',countNoShow);
        	fm.put('showPerc',showPerc);
        	map<string,map<string,decimal>> bm = new map<string,map<string,decimal>>();
        	bm.put(e.Booker__c,fm);
        	mapData.put(dateKey,bm);
		}
This was selected as the best answer
Jonathan KimberlyJonathan Kimberly

I finally decided the best design patter for the nested mapping. I will post my answer here for others, since the responses I get are sometimes too generalized than what is needed MyPrepaidCenter (https://myprepaidcenter.net/).
 
map<string,map<string,map<string,decimal>>> mapData = new map<string,map<string,map<string,decimal>>>();
    //get all events
    for(Event e: eventLst){
        string dateKey = dateMapKey(e.CreatedDate);
        map<string,map<string,decimal>> bmap = new map<string,map<string,decimal>>();
        map<string,decimal> fieldMap = new map<string,decimal>();
        if(mapData.get(dateKey) != null){
            bmap = mapData.get(datekey);
            if(bmap.get(e.Booker__c) != null){
                fieldMap = bmap.get(e.Booker__c);
            }
        }
        decimal countShow=0,countNoShow=0,showPerc=0;
        if(fieldMap.get('countShow') != null){
            countShow = fieldMap.get('countShow');
        }
        if(fieldMap.get('countNoShow') != null){
            countNoShow = fieldMap.get('countNoShow');
        }
        if(fieldMap.get('countNoShow') != null && fieldMap.get('countShow') != null){
            showPerc = fieldMap.get('showPerc');
        }
        if(e.Booking_Status__c == 'Show'){
            countShow++;
        }
        if(e.Booking_Status__c == 'No Show'){
            countNoShow++;
        }
        showPerc = countNoShow != 0 ? (countShow / (countShow + countNoShow)* 100).setScale(1) : 0.0;
        map<string,decimal> fm = new map<string,decimal>();
        fm.put('countShow',countShow);
        fm.put('countNoShow',countNoShow);
        fm.put('showPerc',showPerc);
        map<string,map<string,decimal>> bm = new map<string,map<string,decimal>>();
        bm.put(e.Booker__c,fm);
        mapData.put(dateKey,bm);
    }

 
lan nguyen 46lan nguyen 46
I am a developer, I have founded bestringtone.net website. But unfortunately I can't help you with this question
James GonzalesJames Gonzales
what is needed MyPrepaidCenter (myprepaidcenter.onl (https://myprepaidcenter.onl/))
Devian StudioDevian Studio
You can see details on my website: https://devianstudio.net/
Gameapps BrasilGameapps Brasil
Very useful ìnormations. Thank you so much!
 Vidmate (https://uptofast.com/android/vidmate) at https://uptofast.com/
Jonathan li Kimberly naJonathan li Kimberly na
Cắt Tải Nhạc Chuông Hay Miễn Phí 100% #nhacchuongnet #cubaosao Link truy cập  https://nhacchuong.net Top những bài nhạc hay nhất cho điện thoại, Trang chia sẻ nhạc chuông cho điện thoại iPhone, Android
Yaya SayaYaya Saya
Hi today I am going to show you how to download ringtones for free without advertisement
Simple!
Download Now: https://mp3ringtonesdownload.net
I bet you will enjoy the music ringtones - MP3 Ringtones 888 Plus
ring kbpsring kbps
Your article is very good. Thank you for sharing. I would recommend you a free ringtones (https://ring320kbps.com/) download site. That is https://ring320kbps.com/
liteblue dlliteblue dl
nice information thanks for sharing this information. here we share som sites which help you lot in some works.
liteblue (https://litebluedl.com/)
ncsecu login (https://litebluedl.com/ncsecu-login/)
prepaidgiftbalance (https://prepaidgiftbalancedl.com/)
tamilrockers (https://prosideas.com/)
Nick Wilson 19Nick Wilson 19
Visita il sito web di suonerie telefono (https://suonerietelefono.net/) telefono per scaricare suonerie gratis (https://suonerietelefono.net/)
Nicholas AltimoreNicholas Altimore
Thanks for helping us in this very complicated problem. I urgently needed to group data and you have solved my problem now. Thanks
MY NAME IS Nicholas Altimore (https://www.myprepaidcenter.live) and I am the CEO at myprepaidcenter.
NhacChuong HinhNenNhacChuong HinhNen
Kênh tại nhạc chuông hay nhất (https://nhacchuonghinhnen.com) và hình nền đẹp nhất (https://nhacchuonghinhnen.com/hinh-nen) dành cho điện thoại iphone và android.
william smith 62william smith 62
Yes I am web developer and created a website Myprepaidcenter (https://www.myprepaidcenterx.com/) but unfortunately I am also looking the solution of same problem 
Mayank Sharma 80Mayank Sharma 80
Get the best and latest updated information of phone ringtones here: https://www.indyaspeak.com/ringtones.php
vishal tendulkarvishal tendulkar
Recently got a mobile phone from family mobile or got a new SIM card from myfamilymobile  (http://myfamilymobile.space/)and willing to know how to activate the same, the article will be helpful for you to get the activation process for myfamilymobile products.
nhacchuong123 comnhacchuong123 com
Nghe và Tải nhạc chuông hay nhất miễn phí. Cắt nhạc chuông theo yêu cầu.Website: https://nhacchuong123.com/
Thang Nguyen 60Thang Nguyen 60
Tôi là một nhà phát triển web, mời các bạn gghes thăm website của chúng tôi để tải về những bản nhạc chuông hay nhất miễn phí nhé. Website: https://nhacchuong123.net/
Andrew Piana 1Andrew Piana 1
I finally decided the best design patter for the nested mapping. I will post my answer here for others, since the responses I get are sometimes too generalized than what is needed MyPrepaidCenter (https://www.myprepaid-center.com/).
 
Bum AlexBum Alex
You can download free ringtones for our phones at: https://suonerieandroid.info/
taigame haytaigame hay
TaiGame - Best Games Android Free Download at https://taigame.me/

Best Games For IOS: https://taigame.me/game-hay-ios/
Adam KirchoffAdam Kirchoff
Best way to get prepaid card balance is MyPrepaidCardStatus (https://myprepaidcenter.me), join the portal and activate your card online. 
TrangNhacChuongTrangNhacChuong
Nghe và tải nhạc chuông mp3 hay nhất miễn phí tại: https://trangnhacchuong.com/
Damore JordaneDamore Jordane
Vizit de multe ori https://tonurideapelgratuite.com/ pentru a obține o mulțime de tonuri de apel unice.
Bi Music ChannelBi Music Channel
Hãy ghé thăm website của tôi tại địa chỉ https://nhacchuonghinhnen.com/ để tải nhạc chuông hay (https://nhacchuonghinhnen.com) và hình nền đẹp cho điện thoại nhé!
jonsrn angerangjonsrn angerang
walmartone login (https://wireone.me/)
Activate My Vanilla Debit Card Online (https://myvanilladebitcard.site/)
Duke Energy Login (https://dukeenergybillpay.co/)
Restaurant Surveys (https://surveylila.com/)
Master GarfieldMaster Garfield
worked like a charm for Myprepaidcenter Activation (https://denglobalsip.com/)
Will give it a try for MyprepaidCenter Balance check https://mycenterpre.shop/