Introduction
In real-world business operations, ensuring that shipments are scheduled only on working days (Monday–Friday, excluding holidays) is crucial. If an order is scheduled to ship on a weekend or holiday, the system should automatically adjust it to the next valid business day.
This blog explains an Apex utility class that helps determine whether a given date is a non-working day and adjusts the order shipping date accordingly.
Real-World Scenarios Where This Logic is Needed
Scenario 1: Weekend Order Processing
A company processes an order on Friday, March 29, 2024, with a next-day shipping rule.
- The system calculates the initial shipping date as Saturday, March 30, 2024.
- Since Saturday is a non-working day, the shipping date is moved to Monday, April 1, 2024 (next business day).
Scenario 2: Holiday Shipping Adjustment
A customer places an order on December 23, 2024, and the default shipping date is December 25, 2024 (Christmas, a public holiday).
- Since December 25 is a holiday, the system skips it and selects December 26, 2024 as the shipping date.
Scenario 3: Combination of Holiday and Weekend
An order is scheduled for Friday, July 3, 2026, but July 4 (Saturday) is Independence Day (a holiday).
- The system sees that both Saturday (July 4) and Sunday (July 5) are non-working days.
- It moves the shipping date to Monday, July 6, 2026.
Apex Utility Class Explanation
Now, let’s break down the Apex code that handles these scenarios.
Step 1: Check If a Given Date is a Non-Working Day
The isNotWorkingDay
method determines if a date falls on: Saturday or Sunday
A holiday from the holiday list
public static boolean isNotWorkingDay(Date varDate, Set<Date> holidayDates) {
// Convert Date to DateTime to extract the weekday
DateTime d = varDate;
String day = d.format('EEE'); // Extracts 'Mon', 'Tue', ..., 'Sun'
// Check if the date is Saturday, Sunday, or a holiday
boolean check = day == 'Sat' || day == 'Sun' || (!holidayDates.isEmpty() && holidayDates.contains(varDate));
System.debug('Checking Date: ' + varDate + ' | Is Non-Working Day: ' + check);
return check;
}
Step 2: Adjust the Shipping Date for Orders
The populateShippingDate
method ensures orders are only scheduled on valid business days.
Process:
- Fetch future holidays from Salesforce.
- Store them in a
Set<Date>
for quick lookup. - Loop through each order and adjust the shipping date.
public static void populateShippingDate(List<Order> listOrder, Date varDate) {
List<Exception__c> exceptionList = new List<Exception__c>();
// Query all future holidays from the Salesforce Holiday object
List<Holiday> holidayList = [SELECT Id, ActivityDate FROM Holiday WHERE ActivityDate >= :System.today()];
Set<Date> holidayDates = new Set<Date>();
// Add holidays to the Set for quick lookup
if (!holidayList.isEmpty()) {
for (Holiday h : holidayList) {
holidayDates.add(h.ActivityDate);
}
System.debug('List of Holidays: ' + holidayDates);
}
// Process each order and adjust the shipping date if needed
for (Order o : listOrder) {
// If the calculated shipping date falls on a non-working day, adjust it
while (isNotWorkingDay(varDate, holidayDates)) {
varDate += 1; // Move to the next day
System.debug('Adjusted Shipping Date: ' + varDate);
}
// Here, you can update the order with the final shipping date
o.Shipping_Date__c = varDate;
}
}
}
Example Order Processing Walkthrough
Let’s say we have an order with an initial shipping date of December 25, 2024 (Christmas holiday).
Input:
listOrder
: A list containing 1 order.varDate
:December 25, 2024
.
Process:
isNotWorkingDay(December 25, 2024) → true
(It’s a holiday).- Move to
December 26, 2024
. isNotWorkingDay(December 26, 2024) → false
(It’s a working day).- Final Shipping Date: December 26, 2024 (Updated in the order).
Final Output:
The order now ships on the next valid business day: December 26, 2024.
Key Benefits of This Approach
Prevents order shipments from being scheduled on weekends or holidays
Automatically adjusts shipping dates without manual intervention
Uses Salesforce’s
Holiday
object for dynamic holiday management
Conclusion
This Apex utility class is an essential tool for order fulfillment in Salesforce, ensuring that shipments are always scheduled on valid business days. By leveraging Salesforce’s Holiday
object and Apex’s date manipulation methods, we automate shipping calculations effectively.