Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. public class GroupBy
  2. {
  3. public Map<Id, List<SObject>> ids(SObjectField field, List<SObject> records)
  4. {
  5. Map<Id, List<SObject>> grouped = new Map<Id, List<SObject>>();
  6. for (SObject record : records)
  7. {
  8. Id parentId = (Id)record.get(field);
  9. if (!grouped.containsKey(parentId))
  10. grouped.put(parentId, new List<SObject>());
  11. grouped.get(parentId).add(record);
  12. }
  13. return grouped;
  14. }
  15. }
  16.  
  17. List<ThatObject__c> those; // = ...;
  18. Map<Id, List<ThisObject__c>> byParent = GroupBy.ids(ThatObject__c.Parent__c, those);
  19.  
  20. static List<SObject> genericize(List<SObject> input)
  21. {
  22. List<SObject> output = new List<SObject>();
  23. for (SObject record : input) output.add(record);
  24. return output;
  25. }
  26. List<ThatObject__c> records = genericize([SELECT Id FROM ThisObject__c]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement