0

I want to create a Map in which EntryDate__c should be the Key and rest other data like ProjectName__c, TaskName__c and TimeDuration__c. I want to store in the List but the important thing is that I want a single entry for a particular date.

0: {Id: "a007F00001JiVo9QAF", EntryDate__c: "2018-10-24", ProjectName__c: "Project2", TaskName__c: "Task2", TimeDuration__c: 4}

1: {Id: "a007F00001JiVTzQAN", EntryDate__c: "2018-10-24", ProjectName__c: "Project1", TaskName__c: "Task1", TimeDuration__c: 5.25}

2: {Id: "a007F00001JhJVqQAN", EntryDate__c: "2018-10-17", ProjectName__c: "Project1", TaskName__c: "Task2", TimeDuration__c: 1}

3: {Id: "a007F00001JhLylQAF", EntryDate__c: "2018-10-17", ProjectName__c: "Project1", TaskName__c: "Task1", TimeDuration__c: 0.83}

4: {Id: "a007F00001JhJGqQAN", EntryDate__c: "2018-10-17", P
rojectName__c: "Project1", TaskName__c: "Task1", TimeDuration__c: 7}

5: {Id: "a007F00001Jh5zvQAB", EntryDate__c: "2018-10-16", ProjectName__c: "Project1", TaskName__c: "Task3", TimeDuration__c: 1.75}

6: {Id: "a007F00001Jh5zgQAB", EntryDate__c: "2018-10-16", ProjectName__c: "Project1", TaskName__c: "Task2", TimeDuration__c: 3.75}

7: {Id: "a007F00001Jh5zWQAR", EntryDate__c: "2018-10-16", ProjectName__c: "Project1", TaskName__c: "Task1", TimeDuration__c: 2}

8: {Id: "a007F00001Jh5zRQAR", EntryDate__c: "2018-10-15", ProjectName__c: "Project1", TaskName__c: "Task1", TimeDuration__c: 2}

9: {Id: "a007F00001Jh6UNQAZ", EntryDate__c: "2018-10-12", ProjectName__c: "Project1", TaskName__c: "Task1", TimeDuration__c: 6}

10: {Id: "a007F00001Jh6USQAZ", EntryDate__c: "2018-10-12", ProjectName__c: "Project1", TaskName__c: "Task2", TimeDuration__c: 2.25}

11: {Id: "a007F00001JhXu4QAF", EntryDate__c: "2018-10-05", ProjectName__c: "Project1", TaskName__c: "Task2", TimeDuration__c: 4}

12: {Id: "a007F00001JhWqFQAV", EntryDate__c: "2018-10-02", ProjectName__c: "Project1", TaskName__c: "Task1", TimeDuration__c: 9}
length: 13

the Map structure should be like this

"2018-10-24"    Project2,Task2,4, Project1, Task1,5.25

"2018-10-17"    Project1, Task2,1, Project1, Task1,0.83, Project1, Task2,7,  

Please help me to get the map structure like this.

2

Lets say, this custom object is called Entry__c . In order to have one one entry per EntryDate__c you need to create new wrapper class EntryWrapper.

public EntryWrapper{
    public String projectName {get; set;}
    public String taskName {get; set;}
    public Decimal timeDuration {get; set;}

    public EntryWrapper(Entry__c entry){
        projectName = entry.ProjectName__c;
        taskName = entry.TaskName__c;
        timeDuration = entry.TimeDuration__c;
    }

    public override String toString(){
        return projectName + ', ' + taskName + ',' + String.valueOf(timeDuration);
    }
}

Here override toString method to get output in desired way.

List<Entry__c> entries = new List<Entry__c>();
Map<Date, List<EntryWrapper>> entriesWrapperByEntryDate = new Map<Date, List<EntryWrapper>>();

for(Entry__c entry :entries){
    List<EntryWrapper> entryWrappers = null;
    if(entriesWrapperByEntryDate.containsKey(entry.EntryDate__c)){
        entryWrappers = entriesWrapperByEntryDate.get(entry.EntryDate__c);
    }else{
        entryWrappers = new List<EntryWrapper>();
    }
    entryWrappers.add(
        new EntryWrapper(entry)
        );
    entriesWrapperByEntryDate.put(entry.EntryDate__c, entryWrappers);
}

System.debug(entriesWrapperByEntryDate); // result will be in desired output way
| improve this answer | |
  • Thank you very much Oleksandr Berehovskiy . It's working for me. – Atul Pandey Oct 29 '18 at 7:58
  • Really appreciate your help. – Atul Pandey Oct 29 '18 at 8:22
0

Not clear to me that the toString override is a requirement, so if not this simpler approach without a wrapper class makes sense:

List<Entry__c> entries = ...

Map<Date, List<Entry__c>> byDate = new Map<Date, List<Entry__c>>();

for (Entry__c e : entries) {
    List<Entry__c> forDate = byDate.get(e.EntryDate__c);
    if (forDate == null) {
        forDate = new List<Entry__c>();
        byDate.put(e.EntryDate__c);
    }
    forDate(e);
}
System.debug(JSON.stringify(byDate));
| improve this answer | |
0

There is no need of wrapper. You can simply iterate over the list.

List<Entry__c> entries = new List<Entry__c>();
Map<Date, List<String>> entriesByEntryDate = new Map<Date, List<String>>();
For(EntryDate__c eachRecord : entries){
    if(entriesByEntryDate.containskey(eachRecord.EntryDate__c)){
      List<String> entriesList = new List<String>();
      entriesList = entriesByEntryDate.get(eachRecord.EntryDate__c);
      entriesList.add(eachRecord.ProjectName__c+eachRecord.TaskName__c+String.valueOf(eachRecord.TimeDuration__c));
      entriesByEntryDate.put(eachRecord.EntryDate__c, entriesList);
    }
    else{
     entriesByEntryDate.put(eachRecord.EntryDate__c, new List<String>{eachRecord.ProjectName__c+eachRecord.TaskName__c+String.valueOf(eachRecord.TimeDuration__c)});
    }
}

Check this out.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.