- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
We have a catalog item that has two separate variables sets, one set and one MRVS. In the set (named General contact information) we have a variable 'name' that captures the logged in user's name. In the MRVS (named Course Merge Details) we have a variable that we want to validate called 'who_is_the_instructor'. Both fields are reference fields to the user table.
What we want to validate is that the who_is_the_instructor field is neither blank nor equal to the name of the requestor. We would love to do this check as the user is completing rows in the MRVS, but from what I am reading, you can't do that in a MRVS and need to do it onSubmit of the entire form. Here is my code which is throwing a Javascript error in both cases and I'm not just seeing what I'm missing.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @tcorniels ,
Try to use an onSubmit Catalog Client Script at the Catalog Item Level..MRVS doesn’t support onChange
validation, so the go to approach is:
Create an onSubmit script that applies to the Catalog Item (not the variable set). Parse the MRVS JSON, then loop through each row to validate.
function onSubmit() {
g_form.clearMessages();
var requestor = g_form.getValue('name');
var mrvsValue = g_form.getValue('course_merge_details'); // Your MRVS internal name
if (!mrvsValue || mrvsValue === '[]') {
return true;
}
try {
var rows = JSON.parse(mrvsValue);
for (var i = 0; i < rows.length; i++) {
var instructor = rows[i].who_is_the_instructor;
if (!instructor) {
g_form.addErrorMessage("Row " + (i + 1) + ": Instructor cannot be blank.");
return false;
}
if (instructor === requestor) {
g_form.addErrorMessage("Row " + (i + 1) + ": Instructor cannot be the requestor themselves.");
return false;
}
}
} catch (e) {
g_form.addErrorMessage("Unexpected error validating instructor field.");
return false;
}
return true;
}
f you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
You can use an onChange Catalog Client Script that applies to the MRVS, when the instructor variable changes. Since both are reference variables, it would just look something like this:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (g_service_catalog.parent.getValue('name') == newValue) {
alert('Instructor to add cannot be the same as requestor');
g_form.clearValue('who_is_the_instructor');
}
}
This also clears the invalid instructor value, so if this variable is mandatory, a row cannot be added or edited with a value that matches the Name variable.
You'll also want a script similar to your posted script, but onChange of the Name variable, in the event Name is changed after the MRVS is populated. You can add some alerts to your script to find out why it's not working. At the very least, you need to use the MRVS intenal name in the mrvsData script variable assignment. Since this other script is also onChange, replace your returns with a similar g_form.clearValue('name') approach.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @tcorniels ,
Try to use an onSubmit Catalog Client Script at the Catalog Item Level..MRVS doesn’t support onChange
validation, so the go to approach is:
Create an onSubmit script that applies to the Catalog Item (not the variable set). Parse the MRVS JSON, then loop through each row to validate.
function onSubmit() {
g_form.clearMessages();
var requestor = g_form.getValue('name');
var mrvsValue = g_form.getValue('course_merge_details'); // Your MRVS internal name
if (!mrvsValue || mrvsValue === '[]') {
return true;
}
try {
var rows = JSON.parse(mrvsValue);
for (var i = 0; i < rows.length; i++) {
var instructor = rows[i].who_is_the_instructor;
if (!instructor) {
g_form.addErrorMessage("Row " + (i + 1) + ": Instructor cannot be blank.");
return false;
}
if (instructor === requestor) {
g_form.addErrorMessage("Row " + (i + 1) + ": Instructor cannot be the requestor themselves.");
return false;
}
}
} catch (e) {
g_form.addErrorMessage("Unexpected error validating instructor field.");
return false;
}
return true;
}
f you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @tcorniels
You’re on the right track just a couple of gotchas are tripping you up:
Issues in your script:
- g_form.getValue() on MRVS
- For a Multi-Row Variable Set (MRVS), g_form.getValue('variable_set_name') does not work.
- You need to use g_form.getValue('<internal_name_of_mrvs>'), where the internal name is the variable set’s Name (not the label).
- The return is a JSON string of rows, which you’re parsing correctly.
- MRVS variable naming
- Inside your JSON rows, the keys are the variable names from the MRVS, not the labels.
- So if your MRVS has a variable with name who_is_the_instructor, then you must reference it exactly as row.who_is_the_instructor.
- If your variable name has spaces (like who is the instructor), that will break. Names can’t have spaces — double-check in the MRVS variable definition.
- Comparing reference fields
- Both name (requestor) and who_is_the_instructor are reference fields to sys_user.
- g_form.getValue('name') returns the sys_id of the requestor.
- row.who_is_the_instructor also returns a sys_id.
- So your comparison works if you compare sys_ids.
- If you expected display values (like the user’s full name), you’d need to call g_form.getDisplayValue().
See those corrections:
function onSubmit() {
g_form.clearMessages();
// sys_id of the requestor
var requestor = g_form.getValue('name');
// Internal name of the MRVS (check the MRVS "Name" field in Studio)
var mrvsData = g_form.getValue('course_merge_details');
var mrvsRows = JSON.parse(mrvsData || "[]");
for (var i = 0; i < mrvsRows.length; i++) {
var row = mrvsRows[i];
// Check if 'who_is_the_instructor' is blank
if (!row.who_is_the_instructor) {
g_form.addErrorMessage('Ensure the Who is the instructor field is not blank (Row ' + (i+1) + ')');
return false;
}
// Check that instructor is not the same as requestor
if (row.who_is_the_instructor === requestor) {
g_form.addErrorMessage('Instructor cannot be the same as requestor (Row ' + (i+1) + ')');
return false;
}
}
return true;
}
- Replace course_merge_details with the actual Name of your MRVS (not the label).
- Make sure who_is_the_instructor is the variable name in the MRVS, not the label.
- This must be an onSubmit Catalog Client Script, since per-row validation isn’t supported in MRVS UI.