Validation Script for Multi-Row variable set
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-30-2020 02:09 PM
Hi,
I need to add the validation script on multi-row variable set which will validate the value in the single line text and restrict user to add the row in MRVS. I tried below Catalog Client Script but it is not working
Applies to - A variable set
Type = OnSubmit
function onSubmit() {
var msg = 'You cannot enter a single or double quote or a backslash';
var mrvs = g_form.getValue('IO:0c5fb747db2b8c102cb804c2ca9619d5');
//var mrvs = JSON.parse(g_form.getValue('test_mrvs'));
for(var i=0;i<mrvs.length;i++)
{
if (!isValid(mrvs[i].test_variable)) {
g_form.addErrorMessage(msg);
return false;
}
}
}
function isValid(text) {
var IsValidChar = true;
if (text == null) return;
if (text.indexOf('"') > -1 || text.indexOf('\\') > -1 || text.indexOf('\'') > -1) {
IsValidChar = false;
}
return IsValidChar;
}
- Labels:
-
User Experience and Design

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-30-2020 02:55 PM
How about something like this (caveat - untested code) - remember this script lives on the variable set.
function onChange(control, oldValue, newValue, isLoading) {
if (!isLoading) {
var IsValidChar = true;
g_form.clearMessages();
r = g_form.getValue('your_mvrs_var_name'); //variable in MRVS that needs to be checked
if (r != '') {
if (r.indexOf('"') > -1 || r.indexOf('\\') > -1 || r.indexOf('\') > -1) {
IsValidChar = false;
}
}
if (IsValidChar == false) {
g_form.addErrorMessage('Invalid Character');
g_form.setValue('your_mvrs_var_name', '');
g_form.showFieldMsg('your_mvrs_var_name', 'Invalid Character', 'error');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-01-2020 06:20 AM
This also does not restrict user to add the row even the page has the error. I am looking something onSubmit script of the MRVS so that all the variables available in the MRVS can be validated at one instead of adding separate script for each variable.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-01-2020 07:24 AM
If you validate each row in the mrvs and clear out the values if the validation fails (per row) then the user won't be adding a row with incorrect values. I like to do the validation on the mrvs itself (per row) so the user doesnt need to get all the way to item submission to see there is an issue with a row.
But if you need the onSubmit to process all validation, i think that script will need to run on the item, not the mrvs. I've never had much luck with onSubmit scripts on the set.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2020 08:24 AM
Thanks for the response but from a UI perspective, I was looking to add the validation on the Add button of MRVS, the same way Mandatory UI Policies works on Add button. All other options like adding onSubmit script on catalog item or adding onChange script for the variables can be a good workarounds. I hope you can understand the functionality I am trying to achieve and not sure if ServiceNow supports that or not with New York version.