
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2025 06:57 AM
What I thought was going to be a simple task has me confused so wondering if someone may be able to assist me with this Virtual agent script to be used within a decision.
I need the flow to check the time. If its between 9am-5pm local time, then it takes one decision branch, if its not between those times, it takes another.
Ive tried multiple scripts, but i cannot get it work as I want. Could anyone give me any pointers because doing something like this just doesnt seem to work for me:
(function execute() {
var currentTime = new Date();
var startTime = new Date();
startTime.setHours(9, 0, 0); // 9:00 AM
var endTime = new Date();
endTime.setHours(17, 30, 0); // 5:30 PM
if (currentTime >= startTime && currentTime <= endTime) {
vaVars.businessHours = true;
} else {
vaVars.businessHours = false;
}
})()
Branch condition:
(function execute() {
if(vaVars.businessHours==true){
return true;
}
else{
return false;
}
})()
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2025 08:13 AM
In the end, I decided that I should be using a schedule because of weekends. I added the following as a script and it worked:
(function() {
// Get the current user's local time
var userLocalTime = new GlideDateTime();
// Get the schedule
var scheduleSysId = '4caf307c1bfc3b003faeca217e4bcbcf';
var schedule = new GlideSchedule(scheduleSysId);
// Check if the current time is within the schedule
var isWithinSchedule = schedule.isInSchedule(userLocalTime);
// Set vaVars.businessHours based on the schedule check
if (isWithinSchedule) {
vaVars.businessHours = true;
} else {
vaVars.businessHours = false;
}
})()

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2025 07:25 AM
Maybe try this?
(function execute() {
var currentTime = new Date();
var startTime = new Date();
startTime.setHours(9, 0, 0); // 9:00 AM
var endTime = new Date();
endTime.setHours(17, 30, 0); // 5:30 PM
if (+currentTime >= +startTime && +currentTime <= +endTime) {
vaVars.businessHours = true;
} else {
vaVars.businessHours = false;
}
})()

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2025 08:13 AM
In the end, I decided that I should be using a schedule because of weekends. I added the following as a script and it worked:
(function() {
// Get the current user's local time
var userLocalTime = new GlideDateTime();
// Get the schedule
var scheduleSysId = '4caf307c1bfc3b003faeca217e4bcbcf';
var schedule = new GlideSchedule(scheduleSysId);
// Check if the current time is within the schedule
var isWithinSchedule = schedule.isInSchedule(userLocalTime);
// Set vaVars.businessHours based on the schedule check
if (isWithinSchedule) {
vaVars.businessHours = true;
} else {
vaVars.businessHours = false;
}
})()