How to Select All Multiple Choice Options in an MRVS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello Team,
I have a requirement involving a Multi-Row Variable Set (MRVS) in a Service Catalog item.
For example, I have an MRVS named 'Transport'. Inside the MRVS, there is a Multiple-Choice variable named 'car_names' with around 10 choices:
1. Toyota
2. Nissan
3. Camry
4. Mazda
5. All Cars
The requirement is that when the user selects 'All Cars', all the individual car options should automatically be added/selected in the MRVS.
For example, instead of keeping a row with 'All Cars', the MRVS should populate rows for Toyota, Nissan, Camry, Mazda, etc.
I tried using a Catalog Client Script, but I am having difficulty triggering the logic and updating the MRVS when 'All Cars' is selected from the variable inside the MRVS.
Has anyone implemented a similar 'Select All' functionality for a Multiple-Choice variable inside an MRVS? If so, could you please share the recommended approach or an example?
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello,
Yes, this can be done, but the important point is that an MRVS is stored as JSON rows. When All Cars is selected, you can read the MRVS JSON, remove the All Cars row, add one row for each individual car, and then set the MRVS back with g_form.setValue(). ServiceNow Community examples confirm that an MRVS can be read with g_form.getValue(), parsed with JSON.parse(), modified, and written back with g_form.setValue().
For example, on the Catalog Item, you could use an onChange Client Script for the MRVS and do something along these lines:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading)
return;
var mrvsName = 'transport';
var rows = JSON.parse(g_form.getValue(mrvsName) || '[]');
var newRows = [];
for (var i = 0; i < rows.length; i++) {
if (rows[i].car_names == 'all_cars') {
newRows.push({car_names: 'toyota'});
newRows.push({car_names: 'nissan'});
newRows.push({car_names: 'camry'});
newRows.push({car_names: 'mazda'});
} else {
newRows.push(rows[i]);
}
}
g_form.setValue(mrvsName, JSON.stringify(newRows));
}
You would need to replace the values with the actual internal values of your choices, not their display labels.
One caveat: if the All Cars selection is made inside the MRVS row popup, an MRVS Client Script is generally a better place to detect the change. ServiceNow treats MRVS client scripts differently from normal Catalog Item scripts, and the MRVS onChange event is triggered while editing the row
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Anagha Girme ,
Yes, I implemented a similar requirement using an MRVS onSubmit Catalog Client Script.
1. Catalog Item onLoad Client Script
function onLoad() {
// Make the parent Catalog Item g_form available
// to MRVS scripts in Service Portal
if (this) {
this.cat_g_form = g_form;
}
}
2. MRVS onSubmit Client Script
function onSubmit() {
// Get parent Catalog Item g_form
var parentForm = null;
// Service Portal
if (this && this.cat_g_form) {
parentForm = this.cat_g_form;
}
// Native UI
else if (parent && parent.g_form) {
parentForm = parent.g_form;
}
// Parent form could not be found
if (!parentForm) {
return true;
}
// Get values from the current MRVS row
var car = g_form.getValue('car_name');
var quantity = g_form.getValue('quantity');
// Only process All Cars
if (car != 'all_cars') {
return true;
}
// Get existing MRVS rows
var existing = parentForm.getValue('transport');
var rows = existing ? JSON.parse(existing) : [];
var result = [];
// Keep existing cars
// but remove any existing All Cars row
for (var i = 0; i < rows.length; i++) {
if (rows[i].car_name != 'all_cars') {
result.push(rows[i]);
}
}
// Add Toyota
result.push({
car_name: 'toyota',
quantity: quantity
});
// Add Nissan
result.push({
car_name: 'nissan',
quantity: quantity
});
// Add Camry
result.push({
car_name: 'camry',
quantity: quantity
});
// Add Mazda
result.push({
car_name: 'mazda',
quantity: quantity
});
// Replace the entire MRVS
parentForm.setValue(
'transport',
JSON.stringify(result)
);
// Prevent the original All Cars row
// from being added
return false;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
what's your actual business requirement here?
what purpose adding those rows have?
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
