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
Nandhini S 3Nandhini S 3 

I have a list with 2 rows. I need to group the list based on the createdbyID and the resulting list should have only one record. Is that possible?

Nandhini S 3Nandhini S 3
I mean the resulting list should have only one row
Suraj Tripathi 47Suraj Tripathi 47
Hi Nandhini S 3,

You can do this by using map.Kindly Find the solution.
public class TestApex{
    public static void groupBy(){
        
        List<Account> accoundList = new List<Account>([SELECT Id, Name FROM Account LIMIT 50000]);
        
        Map<DateTime, List<Account>> createdDate_vs_OppList = new Map<DateTime, List<Account>>();
        for(Account acc :accoundList){
            // if key does not contains
            if(!createdDate_vs_OppList.containsKey(acc.createdDate)){
                List<Account> accList = new List<Account>();
                accList.add(acc);
                createdDate_vs_OppList.put(acc.CreatedDate,accList );    
            }
            // key already contains
            else{
                List<Account> accList = new List<Account>();
                accList = createdDate_vs_OppList.get(acc.CreatedDate);
                accList.add(acc);
                createdDate_vs_OppList.put(acc.CreatedDate,accList );    
            }
        }
        //Display Map
        for(DateTime dt : createdDate_vs_OppList.keySet()){
            List<Account> accList = createdDate_vs_OppList.get(dt);
            System.debug('Account Created Date'+dt);
            System.debug('Account List'+accList);
            
        }
    }
}
If you find your Solution than mark as this as a best answer. 

Thanks and Regards
Suraj Tripathi.