public class BatchHealthcareJourneyLastSyncedDate implements Schedulable, Database.Batchable<sObject>{
public void execute(SchedulableContext sc) {
Id batchProcessId = Database.executeBatch(this);
}
public Database.QueryLocator start(Database.BatchableContext bc) {
Map<Id, HealthCare_Journey_Template__c> hcjTempMap = new Map<Id, HealthCare_Journey_Template__c>(
[SELECT Name, Template_Last_Modified__c,
(SELECT Field, NewValue, OldValue, ParentId FROM Histories WHERE Field = 'Template_Last_Modified__c')
FROM HealthCare_Journey_Template__c]);
Set<Id> hcjTempIds = new Set<Id>();
for(HealthCare_Journey_Template__c hcjTmp : hcjTempMap.values()) {
for(HealthCare_Journey_Template__History hcjhist : hcjTmp.Histories) {
if(hcjhist.Field == 'Template_Last_Modified__c'
&& (hcjhist.OldValue != hcjhist.NewValue) && (hcjhist.ParentId == hcjTmp.Id)) {
hcjTempIds.add(hcjTmp.Id);
}
}
}
System.debug('hcjTempIds ' +hcjTempIds);
return Database.getQueryLocator([SELECT Name, Healthcare_Journey_Template__c, Last_Synced_Date__c FROM HealthCare_Journey__c
WHERE Journey_out_of_date__c = true AND Healthcare_Journey_Template__c IN :hcjTempIds]);
}
public void execute(Database.BatchableContext bc, List<HealthCare_Journey__c> scope) {
System.debug('⌗⌗⌗⌗ UPDATED Scope Size ' + scope.size());
List<HealthCare_Journey__c> hcjToUpdate = new List<HealthCare_Journey__c>();
for(HealthCare_Journey__c hcj : scope) {
hcj.Last_Synced_Date__c = System.now();
hcjToUpdate.add(hcj);
}
if(hcjToUpdate.size() > 0) {
update hcjToUpdate;
}
}
public void finish(Database.BatchableContext info) {
System.debug('Done');
}
}
here is my test class:
@isTest
public class BatchHealthcareJourneyLastSyncedDateTest {
@testSetup
static void setup() {
Indication__c ind = new Indication__c(Name = 'test Indication');
insert ind;
Account acc = new Account(Name = 'test Account');
insert acc;
Healthcare_Journey_Template__c hcjTemp = new Healthcare_Journey_Template__c(Name = 'testHcjTemplate',
Template_Last_Modified__c = System.now().addDays(-1),
Indication__c = ind.Id);
insert hcjTemp;
Step_Master__c stpMaster = new Step_Master__c(Name = 'test Step Master');
insert stpMaster;
Sub_Step_Master__c subStpMaster = new Sub_Step_Master__c(Name = 'Bilan sanguin',
Step_Master__c = stpMaster.Id);
insert subStpMaster;
Template_Step__c tstep = new Template_Step__c(Name = 'testTemplateStep',
Step_Number__c = 1,
Step_Master__c = stpMaster.Id,
HealthCare_Journey_Template__c = hcjTemp.Id);
insert tstep;
Template_Sub_Step__c tsubstep = new Template_Sub_Step__c(Name = 'sub Step',
Sub_Step_Number__c = 1,
Sub_Step_Master__c = subStpMaster.Id,
Template_Step__c = tstep.Id);
insert tsubstep;
List<Healthcare_Journey__c> hcjList = new List<Healthcare_Journey__c>();
for(Integer i=0; i<200; i++) {
hcjList.add(new Healthcare_Journey__c(Name = 'test journey ' +i, Last_Synced_Date__c = System.now().addDays(-1),
Healthcare_Journey_Template__c = hcjTemp.Id));
}
insert hcjList;
List<Healthcare_Journey__c> hcjToUpdate = new List<Healthcare_Journey__c>();
for(Healthcare_Journey__c hcj : hcjList) {
hcj.Last_Synced_Date__c = System.now();
hcjToUpdate.add(hcj);
}
update hcjToUpdate;
Test.startTest();
BatchHealthcareJourneyLastSyncedDateTest batchClass = new BatchHealthcareJourneyLastSyncedDateTest();
batchClass.processTemplateUpdate(hcjTemp.Id);
HealthCare_Journey_Template__History[] hcjHist = batchClass.retrieveTemplateHistory(hcjTemp.Id);
Test.stopTest();
System.debug('hcjHist History ' +hcjHist);
System.assertEquals(1, hcjHist.size());
}
public void processTemplateUpdate(Id hcjTempId) {
Healthcare_Journey_Template__c hcjTemp = new Healthcare_Journey_Template__c(Id = hcjTempId, Template_Last_Modified__c = System.now());
update hcjTemp;
}
public HealthCare_Journey_Template__History[] retrieveTemplateHistory(Id hcjTempId) {
List<HealthCare_Journey_Template__History> hcjHist;
if(Test.isRunningTest()) {
hcjHist = new List<HealthCare_Journey_Template__History>{}; //OldValue, NewValue not writeable
hcjHist.add(new HealthCare_Journey_Template__History(ParentId = hcjTempId, Field = 'Template_Last_Modified__c'));
} else
hcjHist = [SELECT ParentId, Field, NewValue, OldValue FROM HealthCare_Journey_Template__History
WHERE ParentId = :hcjTempId];
return hcjHist;
}
@isTest static void test() {
Test.startTest();
BatchHealthcareJourneyLastSyncedDate hcj = new BatchHealthcareJourneyLastSyncedDate();
Id batchId = Database.executeBatch(hcj);
Test.stopTest();
}
}
HI Hermann,The below articles give a good insight into how coverage can be improved https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test