From fde09d2f9c9a3b37063f24e3c75254b180fcaf83 Mon Sep 17 00:00:00 2001 From: Nikolai Skaare Date: Mon, 9 Sep 2024 11:16:35 +0000 Subject: [PATCH 1/2] feat: add segmenting method to record collection --- src/apex/utils/classes/RecordCollection.cls | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/apex/utils/classes/RecordCollection.cls b/src/apex/utils/classes/RecordCollection.cls index d58e216..c145731 100644 --- a/src/apex/utils/classes/RecordCollection.cls +++ b/src/apex/utils/classes/RecordCollection.cls @@ -81,6 +81,21 @@ public virtual class RecordCollection { return new List(new Map(this.records).keySet()); } + public Map> segmentOnField(String field) { + Map> recordsByFieldValue = new Map>; + for (SObject record : this.records) { + Object fieldValue = record.get(field); + + if (!recordsByFieldValue.containsKey(fieldValue)) { + recordsByFieldValue.put(fieldValue, new List()); + } + + recordsByFieldValue.get(fieldValue).add(record); + } + + return recordsByFieldValue; + } + public class AttributeException extends Exception { String attribute; From 93c4281bb3978f30f9a93afc5455c0d86a957b03 Mon Sep 17 00:00:00 2001 From: Nikolai Skaare Date: Tue, 31 Dec 2024 13:46:49 +0100 Subject: [PATCH 2/2] stricter construction of same sobjecttype --- src/apex/utils/classes/RecordCollection.cls | 57 +++++++++++++++------ 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/src/apex/utils/classes/RecordCollection.cls b/src/apex/utils/classes/RecordCollection.cls index c145731..360243e 100644 --- a/src/apex/utils/classes/RecordCollection.cls +++ b/src/apex/utils/classes/RecordCollection.cls @@ -1,42 +1,65 @@ public virtual class RecordCollection { public List records; - public String sObjectType; @TestVisible protected Map attributes = new Map(); - public RecordCollection(String sObjectType, List records) { + public class TypeException extends Exception {} + + public RecordCollection(List records) { + if (records.getSObjectType() == null) { + throw new TypeException('List must have a defined sObjectType'); + } + this.records = records; - this.sObjectType = sObjectType; } - public RecordCollection() { - this(null, new List()); - } + public void add(SObject record) { + if (record.getSObjectType() == null || record.getSObjectType() != this.getSObjectType()) { + throw new TypeException('Can only add record of same sObjectType. Tried adding record of ' + + record.getSObjectType() + + ' to list of ' + + this.getSObjectType() + ); + } - public RecordCollection(List records) { - this(records.get(0)?.getSObjectType().getDescribe().getName(), records); + this.records.add(record); } - public RecordCollection(String sObjectType) { - this(sObjectType, new List()); - } + public void add(List records) { + if (records.getSObjectType() == null || records.getSObjectType() != this.getSObjectType()) { + throw new TypeException('Can only add list of same sObjectType. Tried adding list of ' + + records.getSObjectType() + + ' to list of ' + + this.getSObjectType() + ); + } - public void addRecords(List records) { this.records.addAll(records); } + public void add(RecordCollection collection) { + if (collection.getSObjectType() != this.getSObjectType()) { + throw new TypeException('Can only add collection of same sObjectType. Tried adding collection of ' + + collection.getSObjectType() + + ' to collection of ' + + this.getSObjectType() + ); + } + + this.records.addAll(collection.getRecords()); + } + public List getRecords() { return this.records; } - public String getSObjectType() { - return this.sObjectType; + public Schema.SObjectType getSObjectType() { + return this.records.getSObjectType(); } public Map getRecordTypeInfos() { - return Schema.getGlobalDescribe() - .get(this.sObjectType) + return this.getSObjectType() .getDescribe() .getRecordTypeInfosByDeveloperName(); } @@ -82,7 +105,7 @@ public virtual class RecordCollection { } public Map> segmentOnField(String field) { - Map> recordsByFieldValue = new Map>; + Map> recordsByFieldValue = new Map>(); for (SObject record : this.records) { Object fieldValue = record.get(field);