Skip to content

Commit 084def6

Browse files
committed
1. trigger account trigger logic
2. add custom metadata 3. add test classes
1 parent 90d805b commit 084def6

21 files changed

+16523
-17
lines changed

.prettierrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99
"files": "*.{cmp,page,component}",
1010
"options": { "parser": "html" }
1111
}
12-
]
12+
],
13+
"useTabs": true,
14+
"tabWidth": 4
1315
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public with sharing class AccountTriggerHandler {
2+
public static void saveFieldHistories(
3+
List<Account> newAccounts,
4+
Map<Id, Account> oldMap
5+
) {
6+
FieldTrackerService fts = FieldTrackerService.getInstance('Account');
7+
fts.saveFieldHistories(newAccounts, oldMap);
8+
}
9+
}
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>55.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
@isTest
2+
class AccountTriggerTest {
3+
@TestSetup
4+
static void makeData() {
5+
List<Account> accounts = new List<Account>();
6+
for (Integer i = 0; i < 200; i++) {
7+
Account acc = new Account(
8+
Name = 'Account FTS ' + i,
9+
BillingCountry = 'United States'
10+
);
11+
accounts.add(acc);
12+
}
13+
14+
insert accounts;
15+
}
16+
17+
@IsTest
18+
static void testAfterInsertHistory() {
19+
Set<Id> accountIds = new Map<Id, Account>(
20+
[SELECT Id FROM Account WHERE Name LIKE 'Account FTS %']
21+
)
22+
.keySet();
23+
24+
Test.startTest();
25+
System.assertEquals(
26+
[
27+
SELECT Id
28+
FROM Field_History__c
29+
WHERE
30+
Tracked_Field_API__c = 'BillingCountry'
31+
AND Tracked_Record_Id__c IN :accountIds
32+
AND Old_Value__c = NULL
33+
]
34+
?.size(),
35+
200,
36+
'Field history records not found'
37+
);
38+
Test.stopTest();
39+
}
40+
41+
@IsTest
42+
static void testAfterUpdateHistory() {
43+
Account[] accounts = [
44+
SELECT Id
45+
FROM Account
46+
WHERE Name LIKE 'Account FTS %'
47+
];
48+
49+
Set<Id> accountIds = new Map<Id, Account>(accounts).keySet();
50+
51+
for (Account acc : accounts) {
52+
acc.BillingCountry = 'India';
53+
}
54+
55+
update accounts;
56+
57+
Test.startTest();
58+
System.assertEquals(
59+
[
60+
SELECT Id
61+
FROM Field_History__c
62+
WHERE
63+
Tracked_Field_API__c = 'BillingCountry'
64+
AND Tracked_Record_Id__c IN :accountIds
65+
AND Old_Value__c = 'United States'
66+
]
67+
?.size(),
68+
200,
69+
'Field history records not found'
70+
);
71+
Test.stopTest();
72+
}
73+
}
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>55.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Singleton Class implements the custom field history
3+
tracking dynamically through custom metadata configuration
4+
*/
5+
public with sharing class FieldTrackerService {
6+
// Static members
7+
private static FieldTrackerService instance;
8+
9+
public static FieldTrackerService getInstance(String objectName) {
10+
if (instance == null) {
11+
instance = new FieldTrackerService(objectName);
12+
}
13+
return instance;
14+
}
15+
16+
// instance members
17+
private String objectName;
18+
private Set<String> fields;
19+
private List<Field_Tracker_Field__mdt> trackedFields;
20+
21+
private FieldTrackerService(String objectName) {
22+
this.objectName = objectName;
23+
24+
Field_Tracker_Object__mdt ftObject = Field_Tracker_Object__mdt.getInstance(
25+
objectName
26+
);
27+
trackedFields = [
28+
SELECT Field__c, Field_Label__c
29+
FROM Field_Tracker_Field__mdt
30+
WHERE Field_Tracker_Object__c = :ftObject.Id AND Is_Active__c = TRUE
31+
];
32+
fields = Schema.getGlobalDescribe()
33+
.get(objectName)
34+
.getDescribe()
35+
.fields.getMap()
36+
.keySet();
37+
}
38+
39+
// OldMap should be null in case of Insert trigger.
40+
public void saveFieldHistories(
41+
List<SObject> records,
42+
Map<Id, SObject> oldMap
43+
) {
44+
List<Field_History__c> fieldHistories = new List<Field_History__c>();
45+
Boolean isNew = oldMap == null;
46+
for (SObject record : records) {
47+
SObject oldRecord = oldMap?.get(record.Id);
48+
for (Field_Tracker_Field__mdt trackedField : trackedFields) {
49+
if (
50+
String.isNotBlank(trackedField.Field__c) &&
51+
fields.contains(trackedField.Field__c.toLowerCase()) &&
52+
(isNew ||
53+
oldRecord.get(trackedField.Field__c) !=
54+
record.get(trackedField.Field__c))
55+
) {
56+
fieldHistories.add(
57+
new Field_History__c(
58+
Tracked_Object__c = objectName,
59+
Tracked_Field_Label__c = trackedField.Field_Label__c,
60+
Tracked_Field_API__c = trackedField.Field__c,
61+
Tracked_Record_Id__c = record.Id,
62+
New_Value__c = String.valueOf(
63+
record.get(trackedField.Field__c)
64+
),
65+
Old_Value__c = String.valueOf(
66+
oldRecord?.get(trackedField.Field__c)
67+
)
68+
)
69+
);
70+
}
71+
}
72+
}
73+
74+
insert fieldHistories;
75+
}
76+
}
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>55.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
@IsTest
2+
class FieldTrackerServiceTest {
3+
@TestSetup
4+
static void makeData() {
5+
List<Account> accounts = new List<Account>();
6+
for (Integer i = 0; i < 200; i++) {
7+
Account acc = new Account(
8+
Name = 'Account i' + i,
9+
BillingCountry = 'United States'
10+
);
11+
accounts.add(acc);
12+
}
13+
14+
insert accounts;
15+
}
16+
17+
@IsTest
18+
static void testAccountFTS() {
19+
System.assert(
20+
[SELECT Id FROM Field_Tracker_Field__mdt]?.size() > 0,
21+
'No Custom Metadata records found in Field Tracker Object and Field Tracker Field, please add to pass tests'
22+
);
23+
}
24+
25+
@IsTest
26+
static void testAccountFTSUpdate() {
27+
Account[] accounts = [SELECT id FROM Account];
28+
for (Account acc : accounts) {
29+
acc.BillingCountry = 'India';
30+
}
31+
32+
update accounts;
33+
34+
System.assert(
35+
[
36+
SELECT Id
37+
FROM Field_History__c
38+
WHERE Tracked_Field_API__c = 'BillingCountry'
39+
]
40+
?.size() > 0
41+
);
42+
}
43+
}
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>55.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<CustomMetadata
3+
xmlns="http://soap.sforce.com/2006/04/metadata"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
6+
>
7+
<label>Billing Country</label>
8+
<protected>false</protected>
9+
<values>
10+
<field>Field_Label__c</field>
11+
<value xsi:type="xsd:string">Billing Country</value>
12+
</values>
13+
<values>
14+
<field>Field_Tracker_Object__c</field>
15+
<value xsi:type="xsd:string">Account</value>
16+
</values>
17+
<values>
18+
<field>Field__c</field>
19+
<value xsi:type="xsd:string">BillingCountry</value>
20+
</values>
21+
<values>
22+
<field>Is_Active__c</field>
23+
<value xsi:type="xsd:boolean">true</value>
24+
</values>
25+
</CustomMetadata>

0 commit comments

Comments
 (0)