Test with Multiple Records
By the end of this lesson, you'll be able to:
- Manually verify both overlap scenarios using Execute Anonymous
- Test a batch insert with multiple records at once, not just one record at a time
- Recognize why testing only single-record scenarios would miss real bugs here
Prerequisites: "Bulkify the Logic"
Verifying against an existing record
insert new Leave_Request__c(
Employee_Name__c = 'Amara', Start_Date__c = Date.newInstance(2026, 4, 1),
End_Date__c = Date.newInstance(2026, 4, 5), Status__c = 'Approved'
);
try {
insert new Leave_Request__c(
Employee_Name__c = 'Amara', Start_Date__c = Date.newInstance(2026, 4, 3),
End_Date__c = Date.newInstance(2026, 4, 10), Status__c = 'Pending'
);
System.debug('ERROR: this should have been blocked!');
} catch (DmlException e) {
System.debug('Correctly blocked: ' + e.getMessage());
}
This is exactly Module 26's "Test the Feature" pattern — Execute Anonymous plus a try/catch around the expected DmlException — confirming the new-vs-existing overlap check actually fires.
Verifying a batch insert — the scenario a single-record test can't catch
List<Leave_Request__c> batch = new List<Leave_Request__c>{
new Leave_Request__c(Employee_Name__c = 'Ben', Start_Date__c = Date.newInstance(2026, 5, 1), End_Date__c = Date.newInstance(2026, 5, 5), Status__c = 'Pending'),
new Leave_Request__c(Employee_Name__c = 'Ben', Start_Date__c = Date.newInstance(2026, 5, 3), End_Date__c = Date.newInstance(2026, 5, 8), Status__c = 'Pending')
};
try {
insert batch;
System.debug('ERROR: this should have been blocked!');
} catch (DmlException e) {
System.debug('Correctly blocked: ' + e.getMessage());
}
Testing only single-record inserts (one at a time) would never exercise Lesson 4's new-vs-new comparison at all — that logic only ever runs when multiple records are inserted together in the same transaction. This is exactly why "test with multiple records" earns its own dedicated lesson rather than being folded into the single-record verification.
Exercise
Write an Execute Anonymous snippet that inserts a batch of 2 non-overlapping requests for the same employee and confirms it succeeds without an exception.
Show hint
Use dates that clearly don't overlap, e.g. one in March, one in June.
Test with Multiple Records Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
This lesson verifies the feature specifically with multi-record batches — a single-record test would never catch a new-vs-new overlap bug, since that scenario only exists at the batch level.