- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2025 11:10 PM
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 !
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2025 01:04 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2025 11:33 PM
something like this but please enhance, test and debug further
(function() {
// Input: User sys_id
var userSysId = 'USER_SYS_ID_HERE'; // Replace with actual user sys_id
// Get user record
var userGR = new GlideRecord('sys_user');
if (!userGR.get(userSysId)) {
gs.error('User not found');
return;
}
// Get user's time zone
var userTimeZone = userGR.time_zone;
if (!userTimeZone) {
gs.error('User time zone not set');
return;
}
// Get current time in user's time zone
var userTime = new GlideDateTime();
userTime.setTZ(userTimeZone);
// Get current hour and day of the week in user's time zone
var userHour = userTime.getHourLocalTime();
var userDay = userTime.getDayOfWeekLocalTime();
// Define working hours and days
var startHour = 9;
var endHour = 17;
var workingDays = [1, 2, 3, 4, 5]; // Monday to Friday
// Initialize variables
var availableNow = false;
var nextAvailableTime = new GlideDateTime();
// Check if current time is within working hours and days
if (userHour >= startHour && userHour < endHour && workingDays.includes(userDay)) {
availableNow = true;
} else {
availableNow = false;
// Calculate next available time
if (userHour >= endHour || !workingDays.includes(userDay)) {
// Move to the next working day
do {
userTime.addDaysLocalTime(1);
userDay = userTime.getDayOfWeekLocalTime();
} while (!workingDays.includes(userDay));
// Set next available time to 9:00 AM on the next working day
nextAvailableTime = new GlideDateTime(userTime);
nextAvailableTime.setDisplayValue(nextAvailableTime.getLocalDate() + ' 09:00:00');
} else {
// Set next available time to 9:00 AM on the same day
nextAvailableTime = new GlideDateTime(userTime);
nextAvailableTime.setDisplayValue(nextAvailableTime.getLocalDate() + ' 09:00:00');
}
}
// Output results
gs.info('Available Now: ' + availableNow);
gs.info('Next Available Time: ' + nextAvailableTime.getDisplayValue());
})();
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2025 11:49 PM - edited ‎05-04-2025 11:51 PM
Hi @Ankur Bawiskar
Your code is showing the error message as below. I have a code that is not showing any error but is passing wrong value.
i have tried a lot but not found the solution. thats why I come for help. Please provide tested code from your end if possible. Also I am attaching my code here.
Your code error :
Script execution error: Script Identifier: null.null.script, Error Description: Can't find method com.glide.glideobject.GlideDateTime.setTZ(java.lang.String). (null.null.script; line 21), Script ES Level: 0
Javascript compiler exception: Can't find method com.glide.glideobject.GlideDateTime.setTZ(java.lang.String). (null.null.script; line 21) in: (function() { // Input: User sys_id var userSysId = '15084293872829101ce5fc48dabb3502'; // Replace with actual user sys_id // Get user record var userGR = new GlideRecord('sys_user'); if (!userGR.get(userSysId)) { gs.error('User not found'); return; } // Get user's time zone var userTimeZone = userGR.time_zone; if (!userTimeZone) { gs.error('User time zone not set'); return; } // Get current time in ...
Could not find field delete_recovery in table sys_audit_delete during cascade delete
My script :
var approverId = '5137153cc611227c000bbd1bd8cd2007';
var user = new GlideRecord('sys_user');
if (!user.get(approverId)) {
gs.info('User not found.');
} else {
var tz = user.time_zone || 'UTC';
gs.info('User time zone: ' + tz);
var nowUTC = new GlideDateTime();
gs.info('Current time in UTC: ' + nowUTC.getDisplayValue());
var currentDate = new Date();
var options = { timeZone: tz, hour: '2-digit', minute: '2-digit', second: '2-digit' };
var formattedTime = currentDate.toLocaleString('en-US', options); // Adjust for the user's time zone
gs.info('Current time in user\'s time zone: ' + formattedTime);
var hour = parseInt(formattedTime.split(':')[0]);
var day = currentDate.getDay();
var inWorkingHours = (hour >= 9 && hour < 17);
var inWorkingDays = (day >= 1 && day <= 5);
gs.info('In working hours: ' + inWorkingHours + ' and on working day: ' + inWorkingDays);
if (!user.schedule) {
gs.info('No schedule found, using fallback hours.');
} else {
var scheduleId = user.schedule;
var sched = new GlideSchedule(scheduleId);
var nextScheduleTime = sched.getNextScheduleDateTime(nowUTC);
gs.info('Next available working time (UTC): ' + nextScheduleTime.getDisplayValue());
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2025 12:02 AM
the setTZ accepts timezone from package call
try this and it worked for me in global scope
I set the logged in user's timezone as GMT and in GMT it's now 7am and the next available slot will be 5th May 2025 9AM
So it gave me correct output
(function() {
// Input: User sys_id
var userSysId = gs.getUserID(); // Replace with actual user sys_id
// Get user record
var userGR = new GlideRecord('sys_user');
if (!userGR.get(userSysId)) {
gs.error('User not found');
return;
}
// Get user's time zone
var userTimeZone = userGR.time_zone;
if (!userTimeZone) {
gs.error('User time zone not set');
return;
}
var tz = Packages.java.util.TimeZone.getTimeZone(userTimeZone);
// Get current time in user's time zone
var userTime = new GlideDateTime();
userTime.setTZ(tz);
// Get current hour and day of the week in user's time zone
var userHour = userTime.getHourLocalTime();
var userDay = userTime.getDayOfWeekLocalTime();
// Define working hours and days
var startHour = 9;
var endHour = 17;
var workingDays = [1, 2, 3, 4, 5]; // Monday to Friday
// Initialize variables
var availableNow = false;
var nextAvailableTime = new GlideDateTime();
// Check if current time is within working hours and days
if (userHour >= startHour && userHour < endHour && workingDays.includes(userDay)) {
availableNow = true;
} else {
availableNow = false;
// Calculate next available time
if (userHour >= endHour || !workingDays.includes(userDay)) {
// Move to the next working day
do {
userTime.addDaysLocalTime(1);
userDay = userTime.getDayOfWeekLocalTime();
} while (!workingDays.includes(userDay));
// Set next available time to 9:00 AM on the next working day
nextAvailableTime = new GlideDateTime(userTime);
nextAvailableTime.setDisplayValue(nextAvailableTime.getLocalDate() + ' 09:00:00');
} else {
// Set next available time to 9:00 AM on the same day
nextAvailableTime = new GlideDateTime(userTime);
nextAvailableTime.setDisplayValue(nextAvailableTime.getLocalDate() + ' 09:00:00');
}
}
// Output results
gs.info('Available Now: ' + availableNow);
gs.info('Next Available Time: ' + nextAvailableTime.getDisplayValue());
})();
Output:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2025 12:12 AM
Hi @Ankur Bawiskar
I have tested for Asia/Kolkata user , it still passing 9:00:00 but time is 12:41:00