- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2026 01:05 PM
Hello everyone,
I’m trying to auto-populate a Multi-Row Variable Set to simulate a calendar for a user’s business hours. I would like to use an onLoad script to automatically add multiple rows, one for each day of the week.
How can I write a script that creates these rows and populates each one with the corresponding day and related information, so that the user only needs to adjust the values instead of manually adding a row for each day?
You can see the variable names and the expected result in the screenshots below.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2026 03:06 PM
Hello @MiliReinoso
To achieve below via onLoad Client script:
function onLoad() {
var START_HOUR = 9;
var END_HOUR = 17;
var today = new Date();
// Calculate Monday of the current week
var day = today.getDay(); // Sunday=0, Monday=1
var diffToMonday = (day === 0 ? -6 : 1) - day;
var monday = new Date(today);
monday.setDate(today.getDate() + diffToMonday);
var daysOfWeek = [
{ name: "Monday", offset: 0, isWeekday: true },
{ name: "Tuesday", offset: 1, isWeekday: true },
{ name: "Wednesday", offset: 2, isWeekday: true },
{ name: "Thursday", offset: 3, isWeekday: true },
{ name: "Friday", offset: 4, isWeekday: true },
{ name: "Saturday", offset: 5, isWeekday: false },
{ name: "Sunday", offset: 6, isWeekday: false }
];
var varValues = [];
daysOfWeek.forEach(function (dayObj) {
var date = new Date(monday);
date.setDate(monday.getDate() + dayObj.offset);
var startTime = new Date(date);
startTime.setHours(START_HOUR, 0, 0, 0);
var endTime = new Date(date);
endTime.setHours(END_HOUR, 0, 0, 0);
varValues.push({
days: dayObj.name,
start_time: formatDateTime(startTime),
end_time: formatDateTime(endTime),
select_if_you_work: dayObj.isWeekday ? 'Yes' : 'No'
});
});
g_form.setValue('business_hours', JSON.stringify(varValues));
}
//Format: YYYY-MM-DD HH:mm AM/PM
function formatDateTime(date) {
var yyyy = date.getFullYear();
var mm = String(date.getMonth() + 1).padStart(2, '0');
var dd = String(date.getDate()).padStart(2, '0');
var hours = date.getHours();
var minutes = String(date.getMinutes()).padStart(2, '0');
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12 || 12;
return yyyy + '-' + mm + '-' + dd + ' ' + hours + ':' + minutes + ' ' + ampm;
}
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago - last edited a month ago
Hello @MiliReinoso
To achieve this via onChange and not onLoad Client script as something shown below, then please use the below onChange Client script code:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) { // Prevent running during initial form load
return;
}
// Get the Yes/No variable value
var answer = g_form.getValue('are_you_an_employee');
// If user selects YES
if (answer == 'Yes') {
// Show MRVS
g_form.setDisplay('business_hours', true);
// If MRVS is empty, populate default rows
createBusinessHoursIfEmpty();
}
// If user selects NO or None (blank)
else {
// Hide MRVS section
g_form.setDisplay('business_hours', false);
//g_form.setValue('business_hours', ''); //Clear MVRS or set MVRS as empty if want it to be by uncommenting it.
}
//FUNCTION: Check if MRVS is empty and populate default values
function createBusinessHoursIfEmpty() {
// Get current MRVS value
var existingVal = g_form.getValue('business_hours');
// If already filled, do nothing (avoid overwrite)
if (existingVal && existingVal !== '[]') {
return;
}
// Build default rows
var rows = buildDefaultBusinessHoursRows();
// Set MRVS value as JSON String
g_form.setValue('business_hours', JSON.stringify(rows));
}
//FUNCTION: Build Monday–Sunday default rows
function buildDefaultBusinessHoursRows() {
var START_HOUR = 9; // 9 AM
var END_HOUR = 17; // 5 PM
var today = new Date();
// Find Monday of current week
var day = today.getDay();
var diffToMonday = (day === 0 ? -6 : 1) - day;
var monday = new Date(today);
monday.setDate(today.getDate() + diffToMonday);
var daysOfWeek = [{
name: "Monday",
offset: 0,
isWeekday: true
},
{
name: "Tuesday",
offset: 1,
isWeekday: true
},
{
name: "Wednesday",
offset: 2,
isWeekday: true
},
{
name: "Thursday",
offset: 3,
isWeekday: true
},
{
name: "Friday",
offset: 4,
isWeekday: true
},
{
name: "Saturday",
offset: 5,
isWeekday: false
},
{
name: "Sunday",
offset: 6,
isWeekday: false
}
];
var result = [];
// Build each MRVS row
daysOfWeek.forEach(function(d) {
var date = new Date(monday);
date.setDate(monday.getDate() + d.offset);
var start = new Date(date);
start.setHours(START_HOUR, 0, 0, 0);
var end = new Date(date);
end.setHours(END_HOUR, 0, 0, 0);
result.push({
days: d.name,
start_time: formatDateTime(start),
end_time: formatDateTime(end),
select_if_you_work: d.isWeekday ? 'Yes' : 'No'
});
});
return result;
}
// FUNCTION: Format date for MRVS display
function formatDateTime(date) {
var yyyy = date.getFullYear();
var mm = String(date.getMonth() + 1).padStart(2, '0');
var dd = String(date.getDate()).padStart(2, '0');
var hours = date.getHours();
var minutes = String(date.getMinutes()).padStart(2, '0');
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12 || 12;
return yyyy + '-' + mm + '-' + dd + ' ' + hours + ':' + minutes + ' ' + ampm;
}
// Ensuring MRVS field is visible only when needed
g_form.setDisplay('business_hours', answer == 'Yes');
}
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2026 08:09 PM
Hi @MiliReinoso
Check this link if it helps: Add row onLoad to Multi-Row Variable Set
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2026 08:38 PM
so what did you start and where are you stuck?
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader