In ServiceNow, handling business days calculation for automating tasks such as auto-closing cases can indeed be more complex than dealing with calendar days due to weekends, holidays, and other non-business day exclusions. Since you're already using a Flow to manage this process, I'll guide you on how to approach incorporating business days into your logic with a combination of scripting and scheduling.
Step 1: Update Your Flow's Condition for Business Days
Firstly, it's crucial to accurately calculate "business days ago" since your current condition checks for "calendar days". ServiceNow doesn’t have out-of-the-box support to directly calculate business days in Flows. This typically requires scripting to take into account business days, which considers weekends and optionally holidays.
Suggestion: Use a Scripted Condition or Logic
You will need to create a script within your flow or a business rule that calculates 5 business days from the resolved date considering weekends (and optionally, holidays if your organization maintains a holiday schedule in ServiceNow). You can achieve this by utilizing GlideDateTime operations in a script. This script should determine if the current day is 5 or more business days from the resolved date. Since scripting in Flow Designer directly isn't as straightforward, consider calling a Script Include that performs this calculation.
Step 2: Scheduling the Flow
To schedule the flow to run periodically (e.g., daily) and check for cases that need to be closed, you can utilize the "Scheduled Flow Trigger".
- Open your Flow in Flow Designer,
- in your flow, add a Trigger block at the beginning,
- choose "Scheduled" as the trigger type,
- configure the schedule based on your requirement (for instance, this flow could be scheduled to run every day early in the morning).
Step 3: Holiday Handling (Optional)
To handle holidays, if your organization maintains a holiday schedule, you would need to query that table and exclude those days from your calculation. Again, this can be part of a script include that your flow could call to determine if it's time to auto-close a case.
Example Script Snippet
Below is a very basic JavaScript example for calculating business days between two dates. You would need to modify and expand upon this, especially to include holiday checks.
function calculateBusinessDays(startDate, endDate) {
var count = 0;
var currentDate = new GlideDateTime(startDate);
var end = new GlideDateTime(endDate);
while (currentDate < end) {
var dayOfWeek = currentDate.getDayOfWeek();
if(dayOfWeek != 6 && dayOfWeek != 0){ // Skipping Saturday (6) & Sunday (0)
count++;
}
currentDate.addDays(1);
}
return count;
}
Points to Consider:
- The script above doesn't account for holidays. To include holidays, you'd need to adjust the logic to check against your organization's holiday schedule.
- Depending on your specific requirement, adjust the above logic to decrement count (to find past business days) or expand it for finer control.
- Considerations for time zones and specific working hours if they play into your definition of a business day.
Incorporating business logic into automated workflows can substantially improve operational efficiency but requires careful consideration of time-related calculations and organizational nuances.