Skip to main content

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.
29 件の回答
  1. 2018年12月20日 22:13

    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);

    }

0/9000