Skip to content

Commit

Permalink
Merge pull request #578 from navikt/update_subject_batch
Browse files Browse the repository at this point in the history
update subjects on old active STO/BTO cases batchjob
  • Loading branch information
EirikFladby authored Mar 4, 2024
2 parents 9467d16 + 1288799 commit 42071dd
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
21 changes: 21 additions & 0 deletions force-app/main/default/classes/UpdateCaseSubjectsBatch.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public with sharing class UpdateCaseSubjectsBatch implements Database.Batchable<sObject> {
public Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(
'SELECT STO_ExternalName__c, CRM_Case__c, CRM_Case__r.Subject FROM Thread__c WHERE CRM_Is_Closed__c = FALSE AND CRM_Thread_Type__c IN (\'STO\', \'BTO\') AND CRM_Case__c != NULL AND STO_ExternalName__c != NULL'
);
}
public void execute(Database.BatchableContext BC, List<Thread__c> scope) {
List<Case> casesToUpdate = new List<Case>();

for (Thread__c threadRecord : scope) {
Case caseToUpdate = new Case(Id = threadRecord.CRM_Case__c, Subject = threadRecord.STO_ExternalName__c);
casesToUpdate.add(caseToUpdate);
}

if (!casesToUpdate.isEmpty()) {
Database.update(casesToUpdate);
}
}
public void finish(Database.BatchableContext BC) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>59.0</apiVersion>
<status>Active</status>
</ApexClass>
44 changes: 44 additions & 0 deletions force-app/main/default/classes/UpdateCaseSubjectsBatchTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
@isTest
private class UpdateCaseSubjectsBatchTest {
@isTest
static void testBatchExecution() {
// Create test data - Thread records with associated Cases
List<Thread__c> threads = new List<Thread__c>();
List<Case> cases = new List<Case>();

Case testCase = new Case();
Case testCase2 = new Case();
cases.add(testCase);
cases.add(testCase2);
insert cases;

Thread__c testThread = new Thread__c(
CRM_Case__c = testCase.Id,
CRM_Thread_Type__c = 'STO',
CRM_Type__c = 'STO'
);
Thread__c testThread2 = new Thread__c(
CRM_Case__c = testCase2.Id,
CRM_Thread_Type__c = 'STO',
CRM_Type__c = 'STO'
);
threads.add(testThread);
threads.add(testThread2);
insert threads;

Test.startTest();
System.debug(
[
SELECT STO_ExternalName__c, CRM_Case__c, CRM_Case__r.Subject, CRM_Is_Closed__c, CRM_Thread_Type__c
FROM Thread__c
]
);
UpdateCaseSubjectsBatch batch = new UpdateCaseSubjectsBatch();
Database.executeBatch(batch);
Test.stopTest();

List<Case> updatedCases = [SELECT Id, Subject FROM Case WHERE Id IN :cases];
Assert.areNotEqual(null, updatedCases[0].Subject);
Assert.areNotEqual(null, updatedCases[1].Subject);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>59.0</apiVersion>
<status>Active</status>
</ApexClass>

0 comments on commit 42071dd

Please sign in to comment.