Review and Refactor
By the end of this lesson, you'll be able to:
- Review the complete feature for clarity and duplication
- Extract the repeated overlap-and-block logic into a shared private method
- Confirm the refactor preserves both overlap-detection scenarios
Prerequisites: "Test with Multiple Records"
Spotting the duplication
Looking back at Lesson 5's complete handler: both loops call datesOverlap(...) and then, if true, call addError() with a message — the exact same shape, just with a different pair of records and a different message each time. This is worth naming explicitly, even if extracting it fully would add a parameter for the message.
Extracting a shared check-and-block method
private void blockIfOverlapping(Leave_Request__c reqA, Leave_Request__c reqB, String errorMessage) {
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(errorMessage);
}
}
public void beforeInsert(List<Leave_Request__c> newRequests) {
// ... build employeeNames, query existingRequests exactly as before ...
for (Leave_Request__c newReq : newRequests) {
for (Leave_Request__c existing : existingRequests) {
blockIfOverlapping(newReq, existing, 'This leave request overlaps with an existing request.');
}
}
for (Integer i = 0; i < newRequests.size(); i++) {
for (Integer j = i + 1; j < newRequests.size(); j++) {
blockIfOverlapping(newRequests[i], newRequests[j], 'This request overlaps with another request in the same submission.');
}
}
}
private void blockIfOverlapping(Leave_Request__c reqA, Leave_Request__c reqB, String errorMessage) {—blockIfOverlappingnow holds the one piece of logic that was duplicated across both loops.Boolean sameEmployee = reqA.Employee_Name__c == reqB.Employee_Name__c;— the comparability check, extracted once.Boolean overlaps = datesOverlap(reqA.Start_Date__c, reqA.End_Date__c, reqB.Start_Date__c, reqB.End_Date__c);— the overlap check, extracted once.- (blank line)
if (sameEmployee && overlaps) {— the shared condition.reqB.Start_Date__c.addError(errorMessage);— a parameterized message, since each caller needs a different one.}— closes theif.}— closes the helper.- (blank line)
public void beforeInsert(List<Leave_Request__c> newRequests) {— the method.// ... build employeeNames, query existingRequests exactly as before ...— unchanged setup.- (blank line)
for (Leave_Request__c newReq : newRequests) {— the outer loop, unchanged.for (Leave_Request__c existing : existingRequests) {— the inner loop, unchanged.blockIfOverlapping(newReq, existing, 'This leave request overlaps with an existing request.');— each loop is left doing only what makes it genuinely different: which two Lists it's comparing, and which message to pass.}— closes the inner loop.}— closes the outer loop.- (blank line)
for (Integer i = 0; i < newRequests.size(); i++) {— the new-vs-new outer loop, unchanged.for (Integer j = i + 1; j < newRequests.size(); j++) {— the inner loop, unchanged.blockIfOverlapping(newRequests[i], newRequests[j], 'This request overlaps with another request in the same submission.');— the same shared helper, a different message.}— closes the inner loop.}— closes the outer loop.}— closes the method.
Exactly the same refactoring instinct that closed out every previous project module in this course.
Confirming both scenarios still work
Re-running Lesson 6's @isTest methods against this refactored version should produce identical results — the same two DmlExceptions blocked, the same success case allowed through. This is the whole point of refactoring, one final time: behavior unchanged, structure clearer.
Exercise
Call blockIfOverlapping for two given Leave_Request__c variables reqA and reqB, with the message "Overlap detected."
Show hint
blockIfOverlapping(reqA, reqB, "Overlap detected.");
Review and Refactor 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 closing lesson reviews the finished handler, noticing that both comparison loops repeat a very similar "check overlap, then addError" shape — worth extracting into one shared method.