Intermediate 30 min read

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!
  1. List<Leave_Request__c> batch = new List<Leave_Request__c>{ — two requests being submitted together.
  2. new Leave_Request__c(Employee_Name__c = 'Amara', Start_Date__c = Date.newInstance(2026, 3, 10), End_Date__c = Date.newInstance(2026, 3, 15)), — the first.
  3. new Leave_Request__c(Employee_Name__c = 'Amara', Start_Date__c = Date.newInstance(2026, 3, 12), End_Date__c = Date.newInstance(2026, 3, 20)) — the second, overlapping the first.
  4. }; — closes the list literal.
  5. 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.');
        }
    }
}
  1. for (Integer i = 0; i < newRequests.size(); i++) { — the outer loop.
  2. for (Integer j = i + 1; j < newRequests.size(); j++) { — j = i + 1 (Module 7's classic-for-loop index control) ensures each pair of new requests is only compared once, never against itself.
  3. Leave_Request__c reqA = newRequests[i]; — the first request in the pair.
  4. Leave_Request__c reqB = newRequests[j]; — the second.
  5. (blank line)
  6. Boolean sameEmployee = reqA.Employee_Name__c == reqB.Employee_Name__c; — the same comparability check as before.
  7. Boolean overlaps = datesOverlap(reqA.Start_Date__c, reqA.End_Date__c, reqB.Start_Date__c, reqB.End_Date__c); — this is the same overlap check from Lesson 3, applied to newRequests compared against itself instead of against existingRequests.
  8. (blank line)
  9. if (sameEmployee && overlaps) { — both conditions must be true.
  10. reqB.Start_Date__c.addError('This request overlaps with another request in the same submission.'); — blocks the second request in the pair.
  11. } — closes the if.
  12. } — closes the inner loop.
  13. } — closes the outer loop.

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.

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Handle Overlapping Requests Quiz

1. Why does the new-vs-new comparison start the inner loop at j = i + 1 instead of j = 0?

Log in to submit the quiz and save your score.

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.