Intermediate 30 min read

Enforce Business Rules

By the end of this lesson, you'll be able to:

  • Implement the date-range overlap check from Lesson 1's formula
  • Compare each new request against the pre-fetched existing requests
  • Block an overlapping request with a clear addError() message

Prerequisites: "Build a Bulk-Safe Trigger"

The overlap check as code

private Boolean datesOverlap(Date startA, Date endA, Date startB, Date endB) {
    return startA <= endB && startB <= endA;
}

A direct translation of Lesson 1's formula into a small, well-named private helper — this is Module 12's habit of extracting a clearly-named private method, applied from the very start rather than as an afterthought.

Checking each new request against existing ones

public void beforeInsert(List<Leave_Request__c> newRequests) {
    Set<String> employeeNames = new Set<String>();
    for (Leave_Request__c req : newRequests) {
        employeeNames.add(req.Employee_Name__c);
    }

    List<Leave_Request__c> existingRequests = [
        SELECT Id, Employee_Name__c, Start_Date__c, End_Date__c
        FROM Leave_Request__c
        WHERE Employee_Name__c IN :employeeNames AND Status__c != 'Rejected'
    ];

    for (Leave_Request__c newReq : newRequests) {
        for (Leave_Request__c existing : existingRequests) {
            Boolean sameEmployee = newReq.Employee_Name__c == existing.Employee_Name__c;
            Boolean overlaps = datesOverlap(newReq.Start_Date__c, newReq.End_Date__c, existing.Start_Date__c, existing.End_Date__c);

            if (sameEmployee && overlaps) {
                newReq.Start_Date__c.addError('This leave request overlaps with an existing request.');
            }
        }
    }
}

Two nested loops — both purely in-memory (Module 25's key requirement), no SOQL or DML inside either one. Every new request is checked against every relevant existing request, and addError() blocks any genuine overlap with a clear message.

Why the nested loop is still bulk-safe

Nested loops can look alarming, but bulk-safety is specifically about SOQL queries and DML statements, not about loop nesting itself — Module 25 never said "avoid nested loops," it said "avoid SOQL/DML inside a loop." This nested comparison runs entirely against data already fully loaded in memory (newRequests and existingRequests), so it costs zero additional queries or DML statements no matter how many times it iterates.

Exercise

Write the datesOverlap helper method as a comment-free, standalone method, following the formula from Lesson 1.

Show hint

return startA <= endB && startB <= endA;

APEX

Enforce Business Rules Quiz

1. Is a nested for loop over in-memory Lists a governor-limit risk?

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

This lesson implements the actual overlap rule, comparing each new request against every existing request already fetched for that same employee.