Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: SObject record collection #69

Merged
merged 2 commits into from
Dec 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 54 additions & 16 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 @@ -81,6 +104,21 @@ public virtual class RecordCollection {
return new List<Id>(new Map<Id, SObject>(this.records).keySet());
}

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

if (!recordsByFieldValue.containsKey(fieldValue)) {
recordsByFieldValue.put(fieldValue, new List<SObject>());
}

recordsByFieldValue.get(fieldValue).add(record);
}

return recordsByFieldValue;
}


public class AttributeException extends Exception {
String attribute;
Expand Down
Loading