Skip to content

Commit f5ed37c

Browse files
author
James Simone
committed
Added AccountRepository and bugfix for fieldsets that include SystemModstamp, which has weird casing in Salesforce
1 parent ccc4046 commit f5ed37c

3 files changed

Lines changed: 54 additions & 14 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>38.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@isTest
2+
public class AccountRepository_Tests {
3+
@testSetup
4+
static void setupData() {
5+
List<Account> accounts = new List<Account>();
6+
for(Integer i =0; i <3; i++) {
7+
Account account = new Account();
8+
account.FirstName = 'George' + i;
9+
account.LastName = 'Washington';
10+
account.AladdinCustomerId__c = (Decimal)RandomizerUtils.generateInteger(10) + i;
11+
12+
accounts.add(account);
13+
}
14+
15+
insert accounts;
16+
}
17+
18+
@isTest
19+
static void it_should_return_an_account_by_id() {
20+
Account account = [SELECT Id FROM Account LIMIT 1];
21+
22+
Test.startTest();
23+
Account returnedAccount = new AccountRepository().getById(account.Id);
24+
Test.stopTest();
25+
26+
System.assertEquals(account.Id,returnedAccount.Id);
27+
}
28+
}

src/classes/SObjectRepository.cls

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public abstract class SObjectRepository implements ISObjectRepository {
99
private Map<String, Schema.SObjectField> sobjectTypeFieldMap;
1010
private Set<String> queryFields;
1111
private String query;
12-
private Boolean addCommonQueryFields;
12+
private Boolean shouldAddCommonFields;
1313
private Schema.FieldSet fieldSet;
1414
private List<String> whereClauseList;
1515
private List<String> orderByList;
@@ -20,16 +20,16 @@ public abstract class SObjectRepository implements ISObjectRepository {
2020
this(fieldSet, true);
2121
}
2222

23-
protected SObjectRepository(Schema.FieldSet fieldSet, Boolean addCommonQueryFields) {
24-
this.fieldSet = fieldSet;
25-
this.addCommonQueryFields = addCommonQueryFields;
23+
protected SObjectRepository(Schema.FieldSet fieldSet, Boolean shouldAddCommonFields) {
24+
this.fieldSet = fieldSet;
25+
this.shouldAddCommonFields = shouldAddCommonFields;
2626

27-
this.sobjectType = fieldSet.getSObjectType();
28-
this.sobjectTypeFieldMap = this.sobjectType.getDescribe().fields.getMap();
29-
this.queryFields = new Set<String>();
30-
this.whereClauseList = new List<String>();
31-
this.orderByList = new List<String>();
32-
this.forUpdate = false;
27+
this.sobjectType = fieldSet.getSObjectType();
28+
this.sobjectTypeFieldMap = this.sobjectType.getDescribe().fields.getMap();
29+
this.queryFields = new Set<String>();
30+
this.whereClauseList = new List<String>();
31+
this.orderByList = new List<String>();
32+
this.forUpdate = false;
3333

3434
this.addCommonQueryFields();
3535
this.addFieldSetMembers();
@@ -94,24 +94,31 @@ public abstract class SObjectRepository implements ISObjectRepository {
9494
protected void doDelete(List<SObject> records) {Database.delete(records);}
9595

9696
private void addCommonQueryFields() {
97-
if(!this.addCommonQueryFields) return;
97+
if(!this.shouldAddCommonFields) return;
9898

9999
// Auto-add the common fields that are available for the SObject Type
100-
List<String> commonFieldNameList = new List<String>{
100+
Set<String> commonFieldNameList = new Set<String>{
101101
'Id', 'CaseNumber', 'CreatedById', 'CreatedDate', 'IsClosed', 'LastModifiedById', 'LastModifiedDate',
102102
'Name', 'OwnerId', 'Subject', 'RecordTypeId', 'SystemModStamp'
103103
};
104+
104105
for(String commonFieldName : commonFieldNameList) {
106+
//Verify that the field is available on the object being used
105107
if(!this.sobjectTypeFieldMap.containsKey(commonFieldName)) continue;
106108

107-
this.queryFields.add(commonFieldName);
109+
//Salesforce has some inconsistencies in casing for standard field names. We'll go lowercase
110+
//here and in addFieldSetMembers() to ensure the set is unique
111+
this.queryFields.add(commonFieldName.toLowerCase());
108112
}
109113
}
110114

111115
private void addFieldSetMembers() {
112116
if(this.fieldSet == null) return;
113117

114-
for(Schema.FieldSetMember field : this.fieldSet.getFields()) this.queryFields.add(field.getFieldPath());
118+
for(Schema.FieldSetMember field : this.fieldSet.getFields()) {
119+
//Lowercase here as well to ensure strings added to the set are unique
120+
this.queryFields.add(field.getFieldPath().toLowerCase());
121+
}
115122
}
116123

117124
private SObjectRepository addCondition(Schema.SObjectField field, String operator, String value) {

0 commit comments

Comments
 (0)