Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GroupBy
- {
- public Map<Id, List<SObject>> ids(SObjectField field, List<SObject> records)
- {
- Map<Id, List<SObject>> grouped = new Map<Id, List<SObject>>();
- for (SObject record : records)
- {
- Id parentId = (Id)record.get(field);
- if (!grouped.containsKey(parentId))
- grouped.put(parentId, new List<SObject>());
- grouped.get(parentId).add(record);
- }
- return grouped;
- }
- }
- List<ThatObject__c> those; // = ...;
- Map<Id, List<ThisObject__c>> byParent = GroupBy.ids(ThatObject__c.Parent__c, those);
- static List<SObject> genericize(List<SObject> input)
- {
- List<SObject> output = new List<SObject>();
- for (SObject record : input) output.add(record);
- return output;
- }
- List<ThatObject__c> records = genericize([SELECT Id FROM ThisObject__c]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement