You need to sign in to do that
Don't have an account?
Zahir 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
Notification email object: This is want I want from the above table
Thank you
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
Circuits | Account | Partner name | Email address |
V1C1111 | Xyz1 | Partner1 | Partner1@gmail.com |
V1C1112 | Xyz1 | Partner1 | Partner1@gmail.com |
V1C1113 | Xyz1 | Partner1 | Partner1@gmail.com |
V1C1114 | Company 2 | Partner2 | Partner2@gmail.com |
V1C1115 | Company 2 | Partner2 | Partner2@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, V1C1113 | Xyz1 | Partner1 | Partner1@gmail.com |
V1C1114, V1C1115 | Company 2 | Partner2 | Partner2@gmail.com |
Thank you
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
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
You are a STAR bro ... That's exactly what I wanted ..
Thank you :)