Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Virtual agent script help for business hours

M_iA
Kilo Sage

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;
}
})()
1 ACCEPTED SOLUTION

M_iA
Kilo Sage

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;
    }
})()

View solution in original post

2 REPLIES 2

JenniferRah
Mega Sage
Mega Sage

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;
    }
})()

M_iA
Kilo Sage

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;
    }
})()