Handle Overlapping Requests
By the end of this lesson, you'll be able to:
- Detect overlap between two new requests in the same bulk insert, not just against existing records
- Combine both comparisons (new-vs-existing and new-vs-new) into one coherent check
- Give a distinct, specific error message for each overlap scenario
Prerequisites: "Enforce Business Rules"
The gap: two new requests overlapping each other
List<Leave_Request__c> batch = new List<Leave_Request__c>{
new Leave_Request__c(Employee_Name__c = 'Amara', Start_Date__c = Date.newInstance(2026, 3, 10), End_Date__c = Date.newInstance(2026, 3, 15)),
new Leave_Request__c(Employee_Name__c = 'Amara', Start_Date__c = Date.newInstance(2026, 3, 12), End_Date__c = Date.newInstance(2026, 3, 20))
};
insert batch; // these two overlap each other — but neither exists in the database yet!
The previous lesson's check only compares against existingRequests (already-saved records) — it would miss this case entirely, since neither of these two new requests exists in the database yet to be found by that query.
Adding a new-vs-new comparison
for (Integer i = 0; i < newRequests.size(); i++) {
for (Integer j = i + 1; j < newRequests.size(); j++) {
Leave_Request__c reqA = newRequests[i];
Leave_Request__c reqB = newRequests[j];
Boolean sameEmployee = reqA.Employee_Name__c == reqB.Employee_Name__c;
Boolean overlaps = datesOverlap(reqA.Start_Date__c, reqA.End_Date__c, reqB.Start_Date__c, reqB.End_Date__c);
if (sameEmployee && overlaps) {
reqB.Start_Date__c.addError('This request overlaps with another request in the same submission.');
}
}
}
j = i + 1 (Module 7's classic-for-loop index control) ensures each pair of new requests is only compared once, never against itself — this is the same overlap check from Lesson 3, applied to newRequests compared against itself instead of against existingRequests.
Why the error message is deliberately different
"This leave request overlaps with an existing request" and "This request overlaps with another request in the same submission" describe genuinely different situations — the first means a conflict with something already approved or pending; the second means the batch being submitted right now is internally inconsistent. Giving each its own specific message (Module 26's addError lesson) helps whoever's fixing the problem understand exactly what went wrong, rather than guessing.
Exercise
Given List<Leave_Request__c> requests, write the nested loop structure (without the body) that compares every pair exactly once, never comparing a request to itself.
Show hint
Outer loop i from 0, inner loop j from i + 1.
Handle Overlapping Requests 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
The previous lesson caught overlaps against existing records — this lesson closes the remaining gap: two new requests, inserted together in the same batch, that overlap with each other.