Skip to content

Commit 1918280

Browse files
committed
Lag queueable-jobber for Journal Entries
1 parent 36d8fa8 commit 1918280

8 files changed

+251
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
global class JournalEntryCacheHandler extends MyTriggers {
2+
global override void onAfterInsert() {
3+
Set<Id> conversationNoteIds = new Set<Id>();
4+
for (Journal_Entry__c record : (List<Journal_Entry__c>) records) {
5+
if (record.CRM_Conversation_Note__c != null) {
6+
conversationNoteIds.add(record.CRM_Conversation_Note__c);
7+
}
8+
}
9+
10+
if (!conversationNoteIds.isEmpty()) {
11+
System.enqueueJob(
12+
new ClearHenvendelseCacheQueueable(
13+
conversationNoteIds,
14+
'Conversation_Note__c'
15+
)
16+
);
17+
}
18+
}
19+
20+
global override void onAfterUpdate(Map<Id, SObject> triggerOldMap) {
21+
Set<Id> threadIds = new Set<Id>();
22+
for (Journal_Entry__c record : (List<Journal_Entry__c>) records) {
23+
if (record.CRM_Thread__c != null) {
24+
threadIds.add(record.CRM_Thread__c);
25+
}
26+
}
27+
28+
if (!threadIds.isEmpty()) {
29+
System.enqueueJob(
30+
new ClearHenvendelseCacheQueueable(threadIds, 'Thread__c')
31+
);
32+
}
33+
}
34+
}
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>62.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
@IsTest
2+
public class JournalEntryCacheHandlerTest {
3+
@TestSetup
4+
private static void makeData() {
5+
SharedTestDataFactory.createPersonAccount();
6+
}
7+
8+
@IsTest
9+
static void shouldCreateQueuableJobWhenJournalEntryForConvNoteIsCreated() {
10+
myTriggers.disable(ConvNoteCacheHandler.class);
11+
ApiMock.setTestMock(
12+
HenvendelseCacheCalloutService.SERVICES.DELETE_HENVENDELSE_CACHE_API.name(),
13+
200,
14+
'OK'
15+
);
16+
Id accountId = [SELECT Id FROM Account LIMIT 1].Id;
17+
Conversation_Note__c convNote = (Conversation_Note__c) SharedTestDataFactory.createRecord(
18+
new Conversation_Note__c(
19+
CRM_Account__c = accountId,
20+
CRM_Conversation_Note__c = 'DUMMY TEXT',
21+
CRM_Henvendelse_BehandlingskjedeId__c = '123'
22+
)
23+
);
24+
25+
Test.startTest();
26+
SharedTestDataFactory.createRecord(
27+
new Journal_Entry__c(CRM_Conversation_Note__c = convNote.Id)
28+
);
29+
Test.stopTest();
30+
Assert.areEqual(1, [SELECT COUNT() FROM AsyncApexJob]);
31+
}
32+
33+
@IsTest
34+
static void shouldNotCreateQueuableJobWhenJournalEntryFoThreadIsCreated() {
35+
myTriggers.disable(ConvNoteCacheHandler.class);
36+
ApiMock.setTestMock(
37+
HenvendelseCacheCalloutService.SERVICES.DELETE_HENVENDELSE_CACHE_API.name(),
38+
200,
39+
'OK'
40+
);
41+
42+
Id accountId = [SELECT Id FROM Account LIMIT 1].Id;
43+
Thread__c thread = (Thread__c) SharedTestDataFactory.createRecord(
44+
new Thread__c(CRM_Account__c = accountId, CRM_Type__c = 'STO')
45+
);
46+
47+
Test.startTest();
48+
SharedTestDataFactory.createRecord(
49+
new Journal_Entry__c(CRM_Thread__c = thread.Id)
50+
);
51+
Test.stopTest();
52+
Assert.areEqual(0, [SELECT COUNT() FROM AsyncApexJob]);
53+
}
54+
55+
@IsTest
56+
static void shouldCreateQueuableJobWhenJournalEntryFoThreadIsUpdated() {
57+
myTriggers.disable(ConvNoteCacheHandler.class);
58+
ApiMock.setTestMock(
59+
HenvendelseCacheCalloutService.SERVICES.DELETE_HENVENDELSE_CACHE_API.name(),
60+
200,
61+
'OK'
62+
);
63+
64+
Id accountId = [SELECT Id FROM Account LIMIT 1].Id;
65+
Thread__c thread = (Thread__c) SharedTestDataFactory.createRecord(
66+
new Thread__c(CRM_Account__c = accountId, CRM_Type__c = 'STO')
67+
);
68+
69+
Journal_Entry__c journalEntry = (Journal_Entry__c) SharedTestDataFactory.createRecord(
70+
new Journal_Entry__c()
71+
);
72+
73+
Test.startTest();
74+
journalEntry.CRM_Thread__c = thread.Id;
75+
update journalEntry;
76+
77+
Test.stopTest();
78+
Assert.areEqual(1, [SELECT COUNT() FROM AsyncApexJob]);
79+
}
80+
81+
@IsTest
82+
static void shouldNotCreateQueuableJobWhenJournalEntryForConvNoteIsUpdated() {
83+
myTriggers.disable(ConvNoteCacheHandler.class);
84+
ApiMock.setTestMock(
85+
HenvendelseCacheCalloutService.SERVICES.DELETE_HENVENDELSE_CACHE_API.name(),
86+
200,
87+
'OK'
88+
);
89+
Id accountId = [SELECT Id FROM Account LIMIT 1].Id;
90+
Conversation_Note__c convNote = (Conversation_Note__c) SharedTestDataFactory.createRecord(
91+
new Conversation_Note__c(
92+
CRM_Account__c = accountId,
93+
CRM_Conversation_Note__c = 'DUMMY TEXT',
94+
CRM_Henvendelse_BehandlingskjedeId__c = '123'
95+
)
96+
);
97+
98+
Journal_Entry__c journalEntry = (Journal_Entry__c) SharedTestDataFactory.createRecord(
99+
new Journal_Entry__c()
100+
);
101+
102+
Test.startTest();
103+
journalEntry.CRM_Conversation_Note__c = convNote.Id;
104+
update journalEntry;
105+
106+
Test.stopTest();
107+
Assert.areEqual(0, [SELECT COUNT() FROM AsyncApexJob]);
108+
}
109+
}
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>62.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<CustomMetadata xmlns="http://soap.sforce.com/2006/04/metadata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3+
<label>Henvendelse Cache Insert Journal Entry</label>
4+
<protected>false</protected>
5+
<values>
6+
<field>Active__c</field>
7+
<value xsi:type="xsd:boolean">true</value>
8+
</values>
9+
<values>
10+
<field>ClassNamespacePrefix__c</field>
11+
<value xsi:nil="true"/>
12+
</values>
13+
<values>
14+
<field>Class__c</field>
15+
<value xsi:type="xsd:string">JournalEntryCacheHandler</value>
16+
</values>
17+
<values>
18+
<field>Description__c</field>
19+
<value xsi:type="xsd:string">Initiate caching for new conversation notes</value>
20+
</values>
21+
<values>
22+
<field>Event__c</field>
23+
<value xsi:type="xsd:string">AFTER_INSERT</value>
24+
</values>
25+
<values>
26+
<field>IsBypassAllowed__c</field>
27+
<value xsi:type="xsd:boolean">false</value>
28+
</values>
29+
<values>
30+
<field>Order__c</field>
31+
<value xsi:type="xsd:double">950.0</value>
32+
</values>
33+
<values>
34+
<field>sObjectAPIName__c</field>
35+
<value xsi:nil="true"/>
36+
</values>
37+
<values>
38+
<field>sObject__c</field>
39+
<value xsi:type="xsd:string">Journal_Entry__c</value>
40+
</values>
41+
</CustomMetadata>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<CustomMetadata xmlns="http://soap.sforce.com/2006/04/metadata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3+
<label>Henvendelse Cache Update Journal Entry</label>
4+
<protected>false</protected>
5+
<values>
6+
<field>Active__c</field>
7+
<value xsi:type="xsd:boolean">true</value>
8+
</values>
9+
<values>
10+
<field>ClassNamespacePrefix__c</field>
11+
<value xsi:nil="true"/>
12+
</values>
13+
<values>
14+
<field>Class__c</field>
15+
<value xsi:type="xsd:string">JournalEntryCacheHandler</value>
16+
</values>
17+
<values>
18+
<field>Description__c</field>
19+
<value xsi:type="xsd:string">Initiate caching for updated conversation notes</value>
20+
</values>
21+
<values>
22+
<field>Event__c</field>
23+
<value xsi:type="xsd:string">AFTER_UPDATE</value>
24+
</values>
25+
<values>
26+
<field>IsBypassAllowed__c</field>
27+
<value xsi:type="xsd:boolean">false</value>
28+
</values>
29+
<values>
30+
<field>Order__c</field>
31+
<value xsi:type="xsd:double">950.0</value>
32+
</values>
33+
<values>
34+
<field>sObjectAPIName__c</field>
35+
<value xsi:nil="true"/>
36+
</values>
37+
<values>
38+
<field>sObject__c</field>
39+
<value xsi:type="xsd:string">Journal_Entry__c</value>
40+
</values>
41+
</CustomMetadata>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
trigger JournalEntryTrigger on Journal_Entry__c(
2+
before insert,
3+
before update,
4+
before delete,
5+
after insert,
6+
after update,
7+
after delete,
8+
after undelete
9+
) {
10+
MyTriggers.run();
11+
}
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+
<ApexTrigger xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>62.0</apiVersion>
4+
<status>Active</status>
5+
</ApexTrigger>

0 commit comments

Comments
 (0)