Could you please provide me a scripts - Background code for below scanario.

Chandra18
Mega Sage

Hello Expert,

Please give me the scripts - background code for below scanario. I will be thankful to you.

scanario :
1. Get a user time zone by his/her sys_id and take 2 variable "availableNow", "nextAvailableTime".
2. Check if the time zone of the user is in 9:00 am - 5:00 pm.

3. Check in their calender what is the day right now.
4. If the user time is 9:00am - 5:00 pm in their time zone AND the day is Moday - Friday
Set "availableNow" = 'True'; otherwise False and how to find next available time.

Thank you !


1 ACCEPTED SOLUTION

Robert H
Mega Sage

Hello @Chandra18 ,

 

Here is a quick and easy script to get this information.

 

// SET THIS TO THE USER'S SYS_ID
var USER_SYS_ID = '...';

// this is the OOTB 'Workday 9:00 - 5:00' Schedule in the cmn_schedule table
var SCHEDULE_SYS_ID = '38f9a4e0c0a801640010a471b72952ba';

var timeZone = gs.getSysTimeZone();
var now  = new GlideDateTime();
var grUser = new GlideRecord('sys_user');
if (grUser.get(USER_SYS_ID) && grUser.time_zone) {
	timeZone = grUser.getValue('time_zone');
}
var schedule = new GlideSchedule(SCHEDULE_SYS_ID, timeZone);

var availableNow = schedule.isInSchedule(now);
if (!availableNow) {
	now.add(schedule.whenNext(now));
}
now.setTimeZone(timeZone);
var nextAvailableTime = now.getDisplayValue();

gs.info('Available now: ' + availableNow);
gs.info('Next available time: ' + nextAvailableTime);

 

Output for a user in India on Monday 1:31pm:

Available now: true
Next available time: 2025-05-05 13:31:28

 

Output for a user in Los Angeles at the same moment (Monday 1:01am):

Available now: false
Next available time: 2025-05-05 09:00:00

 

Regards,

Robert

View solution in original post

9 REPLIES 9

@Chandra18 

I shared the sample script, please enhance

Also why not to use schedule for this calculation?

It will be much easier for you to determine the next available time

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Chandra18 

check these links

Get next business day (date/time - based on schedule) 

Calculate next date based on schedule 

Find next business day 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Robert H
Mega Sage

Hello @Chandra18 ,

 

Here is a quick and easy script to get this information.

 

// SET THIS TO THE USER'S SYS_ID
var USER_SYS_ID = '...';

// this is the OOTB 'Workday 9:00 - 5:00' Schedule in the cmn_schedule table
var SCHEDULE_SYS_ID = '38f9a4e0c0a801640010a471b72952ba';

var timeZone = gs.getSysTimeZone();
var now  = new GlideDateTime();
var grUser = new GlideRecord('sys_user');
if (grUser.get(USER_SYS_ID) && grUser.time_zone) {
	timeZone = grUser.getValue('time_zone');
}
var schedule = new GlideSchedule(SCHEDULE_SYS_ID, timeZone);

var availableNow = schedule.isInSchedule(now);
if (!availableNow) {
	now.add(schedule.whenNext(now));
}
now.setTimeZone(timeZone);
var nextAvailableTime = now.getDisplayValue();

gs.info('Available now: ' + availableNow);
gs.info('Next available time: ' + nextAvailableTime);

 

Output for a user in India on Monday 1:31pm:

Available now: true
Next available time: 2025-05-05 13:31:28

 

Output for a user in Los Angeles at the same moment (Monday 1:01am):

Available now: false
Next available time: 2025-05-05 09:00:00

 

Regards,

Robert

Thank You So Much @Robert H  for your help.

Could you please add a gs.info for show user current time that will be help for testing with evidance. 

Hello @Chandra18 ,

 

Sure, you can add the following at the end of the script:

 

function getLocalTime(timeZone) {
	var now = new GlideDateTime();
	now.setTimeZone(timeZone);
	return now.getDisplayValue();
}

gs.info(gs.getMessage('Local time in {0} is: {1}', [timeZone, getLocalTime(timeZone)]));

 

Output:

Local time in America/Los_Angeles is: 2025-05-05 01:39:54

 

Regards,

Robert