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
Zahir BasithZahir Basith 

Looping through a list of records and grouping based on a field

Hello Dev friends, 

I have a custom object called notification details which will have all the circuits effected for a partner. The point to note is that, We will have many of these circuits belonging to the same partner . I want to prepare a notification email record per partner which will have all circuits belonging to a partner.
What is the efficient way of coding this in apex, I would like to get an expert opinion before I start coding my own way J

Notification detail object
CircuitsAccountPartner nameEmail address
V1C1111Xyz1Partner1Partner1@gmail.com
V1C1112Xyz1Partner1Partner1@gmail.com
V1C1113Xyz1Partner1Partner1@gmail.com
V1C1114Company 2Partner2Partner2@gmail.com
V1C1115Company 2Partner2Partner2@gmail.com
 
Notification email object: This is want I want from the above table
Circuits (text field)Account (Lookup)Partner name (Lookup)Email address (Text field)
V1C1111, V1C1112, V1C1113Xyz1Partner1Partner1@gmail.com
V1C1114, V1C1115Company 2Partner2Partner2@gmail.com

Thank you 
Best Answer chosen by Zahir Basith
PawanKumarPawanKumar
Hi Zahir,

This is what i can predict.

// get all unique email id for Partner
AggregateResult[] groupedResults = [Select Count(Id), Emailaddress from Notification Group By Emailaddress];
List < String > partnerEmailList = new List < String > ();
for (AggregateResult ar: groupedResults) {
 partnerEmailList.add(ar.get('Emailaddress'));
}

// Now query
List < Notification > notifyList = [Select Id, AccountName, PartnerName, emailAddress, circuitName from Notification where emailAddress IN: partnerEmailList order by emailAddress];

// Map<String,PartnerWrapper> partnerDetailEmailMap = new Map<String,PartnerWrapper>();
for (Notification notifyRecord: notifyList) {

 // partner exist in map
 if (partnerDetailEmailMap.containsKey(notifyRecord.emailAddress)) {
  PartnerWrapper existingPartner = partnerDetailEmailMap.get(notifyRecord.emailAddress);
  List < String > crctList = existingPartner.circuitList;
  crctList.add(notifyRecord.circuitName);
  existingPartner.circuitList = crctList;

  // add put back to reflect the new circuit name.
  partnerDetailEmailMap.put(notifyRecord.emailAddress, existingPartner);

 } else {
  PartnerWrapper partnerWrapperRecord = new PartnerWrapper();
  partnerWrapperRecord.partnerName = notifyRecord.AccountName;
  partnerWrapperRecord.PartnerName = notifyRecord.PartnerName;
  partnerWrapperRecord.emailAddress = notifyRecord.emailAddress;
  List < String > circuits = new List < String > ();
  circuits.add(notifyRecord.circuitName);
  partnerWrapperRecord.circuitList = circuits;

  // add to map
  partnerDetailEmailMap.put(notifyRecord.emailAddress, partnerWrapperRecord);
 }

} // end for

// create this pojo class
public Class PartnerWrapper{
public string partnerName{get;set;}
public string accountName{get;set;}
public string emailAddress{get;set;}
public List<String> circuitList{get;set;}
}

Regards,
Pawan Kumar

All Answers

PawanKumarPawanKumar
Hi Zahir,

This is what i can predict.

// get all unique email id for Partner
AggregateResult[] groupedResults = [Select Count(Id), Emailaddress from Notification Group By Emailaddress];
List < String > partnerEmailList = new List < String > ();
for (AggregateResult ar: groupedResults) {
 partnerEmailList.add(ar.get('Emailaddress'));
}

// Now query
List < Notification > notifyList = [Select Id, AccountName, PartnerName, emailAddress, circuitName from Notification where emailAddress IN: partnerEmailList order by emailAddress];

// Map<String,PartnerWrapper> partnerDetailEmailMap = new Map<String,PartnerWrapper>();
for (Notification notifyRecord: notifyList) {

 // partner exist in map
 if (partnerDetailEmailMap.containsKey(notifyRecord.emailAddress)) {
  PartnerWrapper existingPartner = partnerDetailEmailMap.get(notifyRecord.emailAddress);
  List < String > crctList = existingPartner.circuitList;
  crctList.add(notifyRecord.circuitName);
  existingPartner.circuitList = crctList;

  // add put back to reflect the new circuit name.
  partnerDetailEmailMap.put(notifyRecord.emailAddress, existingPartner);

 } else {
  PartnerWrapper partnerWrapperRecord = new PartnerWrapper();
  partnerWrapperRecord.partnerName = notifyRecord.AccountName;
  partnerWrapperRecord.PartnerName = notifyRecord.PartnerName;
  partnerWrapperRecord.emailAddress = notifyRecord.emailAddress;
  List < String > circuits = new List < String > ();
  circuits.add(notifyRecord.circuitName);
  partnerWrapperRecord.circuitList = circuits;

  // add to map
  partnerDetailEmailMap.put(notifyRecord.emailAddress, partnerWrapperRecord);
 }

} // end for

// create this pojo class
public Class PartnerWrapper{
public string partnerName{get;set;}
public string accountName{get;set;}
public string emailAddress{get;set;}
public List<String> circuitList{get;set;}
}

Regards,
Pawan Kumar
This was selected as the best answer
Zahir BasithZahir Basith
Hi Pawan,

You are a STAR bro ... That's exactly what I wanted ..
Thank you  :)