Skip to content

Commit

Permalink
stricter construction of same sobjecttype
Browse files Browse the repository at this point in the history
  • Loading branch information
nkskaare committed Dec 31, 2024
1 parent fde09d2 commit 93c4281
Showing 1 changed file with 40 additions and 17 deletions.
57 changes: 40 additions & 17 deletions src/apex/utils/classes/RecordCollection.cls
Original file line number Diff line number Diff line change
@@ -1,42 +1,65 @@
public virtual class RecordCollection {
public List<SObject> records;
public String sObjectType;

@TestVisible
protected Map<String, Object> attributes = new Map<String, Object>();

public RecordCollection(String sObjectType, List<SObject> records) {
public class TypeException extends Exception {}

public RecordCollection(List<SObject> 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<SObject>());
}
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<SObject> records) {
this(records.get(0)?.getSObjectType().getDescribe().getName(), records);
this.records.add(record);
}

public RecordCollection(String sObjectType) {
this(sObjectType, new List<SObject>());
}
public void add(List<SObject> 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<SObject> 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<SObject> getRecords() {
return this.records;
}

public String getSObjectType() {
return this.sObjectType;
public Schema.SObjectType getSObjectType() {
return this.records.getSObjectType();
}

public Map<String, Schema.RecordTypeInfo> getRecordTypeInfos() {
return Schema.getGlobalDescribe()
.get(this.sObjectType)
return this.getSObjectType()
.getDescribe()
.getRecordTypeInfosByDeveloperName();
}
Expand Down Expand Up @@ -82,7 +105,7 @@ public virtual class RecordCollection {
}

public Map<Object, List<SObject>> segmentOnField(String field) {
Map<Object, List<SObject>> recordsByFieldValue = new Map<Object, List<SObject>>;
Map<Object, List<SObject>> recordsByFieldValue = new Map<Object, List<SObject>>();
for (SObject record : this.records) {
Object fieldValue = record.get(field);

Expand Down

0 comments on commit 93c4281

Please sign in to comment.