Help needed with checks on a multi row variable set

snow_beginner
Mega Guru

Hi,

The requirement was to create a field on a catalog item where a user can select up to 7 dates for a planned outage.

 

I made a multi row variable set (MRVS) to fulfill this requirement and added the start and end date/time variables to the MRVS. So far this is working well.

 

However, the requirement is that the user should not be able to add more than 7 dates and I also need something to check if there is any overlap between the selected dates/times. How can I achieve this?

 

Is this something that can be done via a catalog client script? and if so how? 

 

is there a better solution than a MRVS for the requirement to start with?

 

Thanks!

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@snow_beginner 

if you want multiple rows for user to enter then you should use MRVS, if not then single row variable set and have 2 variables directly.

To allow only 7 rows you can max_rows=7 in the MRVS variable set

AnkurBawiskar_0-1746188164729.png

 

It all depends on your customer requirement

Now coming to your validation, you can use onSubmit catalog client script which applies to MRVS and then check if it's more than 7 days of Start date

Note: Use this if both are date/time type

function onSubmit() {

    var start = g_form.getValue('start_date');
    var end = g_form.getValue('end_date');
    var minutes = 10080; // 7 days is 10080 minutes

    var startDt = new Date(getDateFromFormat(start, g_user_date_time_format)).getTime() + minutes * 60000; // add minutes to date and then compare
    var endDt = new Date(getDateFromFormat(end, g_user_date_time_format)).getTime();

    if (endDt < startDt) {
        alert('End date should be 7 days more than Start Date');
        return false;
    }
}

Note: Use this if it's date type

function onSubmit() {

    var start = g_form.getValue('start_date');
    var end = g_form.getValue('end_date');
    var minutes = 10080; // 7 days is 10080 minutes

    var startDt = new Date(getDateFromFormat(start, g_user_date_format)).getTime() + minutes * 60000; // add minutes to date and then compare
    var endDt = new Date(getDateFromFormat(end, g_user_date_format)).getTime();

    if (endDt < startDt) {
        alert('End date should be 7 days more than Start Date');
        return false;
    }
}

AnkurBawiskar_1-1746186367951.png

 

AnkurBawiskar_0-1746186281788.png

 

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

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@snow_beginner 

if you want multiple rows for user to enter then you should use MRVS, if not then single row variable set and have 2 variables directly.

To allow only 7 rows you can max_rows=7 in the MRVS variable set

AnkurBawiskar_0-1746188164729.png

 

It all depends on your customer requirement

Now coming to your validation, you can use onSubmit catalog client script which applies to MRVS and then check if it's more than 7 days of Start date

Note: Use this if both are date/time type

function onSubmit() {

    var start = g_form.getValue('start_date');
    var end = g_form.getValue('end_date');
    var minutes = 10080; // 7 days is 10080 minutes

    var startDt = new Date(getDateFromFormat(start, g_user_date_time_format)).getTime() + minutes * 60000; // add minutes to date and then compare
    var endDt = new Date(getDateFromFormat(end, g_user_date_time_format)).getTime();

    if (endDt < startDt) {
        alert('End date should be 7 days more than Start Date');
        return false;
    }
}

Note: Use this if it's date type

function onSubmit() {

    var start = g_form.getValue('start_date');
    var end = g_form.getValue('end_date');
    var minutes = 10080; // 7 days is 10080 minutes

    var startDt = new Date(getDateFromFormat(start, g_user_date_format)).getTime() + minutes * 60000; // add minutes to date and then compare
    var endDt = new Date(getDateFromFormat(end, g_user_date_format)).getTime();

    if (endDt < startDt) {
        alert('End date should be 7 days more than Start Date');
        return false;
    }
}

AnkurBawiskar_1-1746186367951.png

 

AnkurBawiskar_0-1746186281788.png

 

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

Hi @Ankur Bawiskar thanks so much for your help, that is exactly what I needed.

@snow_beginner

Glad to help

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

GopikaP
Mega Sage

Hi @snow_beginner , for user not be able to add more than 7 dates - 

 

  • Inside the Multi-Row Variable Set, there is a field called "Variable Set attributes" (set_attributes). Set its value to max_rows=7. 
  • Ensure there are no spaces between the equal sign and the number. 

If the field "Variable Set attributes" not available in the form, you can bring it to the form by editing the form layout. 

For validating the overlapping, are you looking for same duplicate entries or the time overlaps?