From 643cca1f9edf15247373f7aae15ea89eda2d5690 Mon Sep 17 00:00:00 2001 From: hanna-g-sn Date: Tue, 21 Oct 2025 08:56:43 +0100 Subject: [PATCH 1/3] Add README for MRVS normalisation client script This README explains how to normalise and reset MRVS rows based on changes to a controlling variable, detailing setup and usage. --- .../README.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Client-Side Components/Catalog Client Script/Normalise and Reset a MRVS based on Variable Changes/README.md diff --git a/Client-Side Components/Catalog Client Script/Normalise and Reset a MRVS based on Variable Changes/README.md b/Client-Side Components/Catalog Client Script/Normalise and Reset a MRVS based on Variable Changes/README.md new file mode 100644 index 0000000000..bff708fbe8 --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Normalise and Reset a MRVS based on Variable Changes/README.md @@ -0,0 +1,31 @@ +# MRVS - Normalise and Reset Rows on Change + +## What this solves +When a controlling variable changes (for example, Environment), existing MRVS rows may no longer be valid. This client script: +- Clears or normalises specific MRVS columns +- Deduplicates rows +- Optionally sorts rows for a cleaner UX +- Works entirely client-side using MRVS JSON + +## Where to use +Catalog Item → OnChange client script on your controlling variable. + +## How it works +- Reads the MRVS value as JSON via `g_form.getValue('my_mrvs')` +- Applies transforms (clear columns, unique by key, sort) +- Writes back the JSON with `g_form.setValue('my_mrvs', JSON.stringify(rows))` + +## Setup +1. Replace `CONTROLLING_VARIABLE` with your variable name. +2. Replace `MY_MRVS` with your MRVS variable name. +3. Adjust `COLUMNS_TO_CLEAR`, `UNIQUE_KEY`, and `SORT_BY` as needed. + +## Notes +- To clear the MRVS entirely, set `rows = []` before `setValue`. +- Works with Catalog Client Scripts; no server call required. + +## References +- GlideForm API (client): `getValue`, `setValue`, `clearValue` + https://www.servicenow.com/docs/bundle/zurich-api-reference/page/app-store/dev_portal/API_reference/GlideForm/concept/c_GlideFormAPI.html +- Working with MRVS values on the client (community examples) + https://www.servicenow.com/community/developer-articles/accessing-multi-row-variable-set-value-outside-the-multi-row/ta-p/2308876 From fc9af1931fec2db9d6f6aa2a5551e03d802ca55c Mon Sep 17 00:00:00 2001 From: hanna-g-sn Date: Tue, 21 Oct 2025 08:57:53 +0100 Subject: [PATCH 2/3] Add MRVS normalization and reset on change --- .../mrvs_normalise_onchange.js | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Client-Side Components/Catalog Client Script/Normalise and Reset a MRVS based on Variable Changes/mrvs_normalise_onchange.js diff --git a/Client-Side Components/Catalog Client Script/Normalise and Reset a MRVS based on Variable Changes/mrvs_normalise_onchange.js b/Client-Side Components/Catalog Client Script/Normalise and Reset a MRVS based on Variable Changes/mrvs_normalise_onchange.js new file mode 100644 index 0000000000..1bbf3695ee --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Normalise and Reset a MRVS based on Variable Changes/mrvs_normalise_onchange.js @@ -0,0 +1,45 @@ +function onChange(control, oldValue, newValue, isLoading) { + if (isLoading) return; + + var MRVS_NAME = 'MY_MRVS'; // your MRVS variable name + var COLUMNS_TO_CLEAR = ['env', 'owner']; // MRVS column names to clear + var UNIQUE_KEY = 'hostname'; // MRVS column that should be unique + var SORT_BY = 'hostname'; // MRVS column to sort by + + try { + var raw = g_form.getValue(MRVS_NAME); + var rows = raw ? JSON.parse(raw) : []; + if (!Array.isArray(rows)) rows = []; + + // Clear specified columns + rows.forEach(function(row) { + COLUMNS_TO_CLEAR.forEach(function(col) { if (row.hasOwnProperty(col)) row[col] = ''; }); + }); + + // Deduplicate by UNIQUE_KEY + if (UNIQUE_KEY) { + var seen = {}; + rows = rows.filter(function(row) { + var key = String(row[UNIQUE_KEY] || '').toLowerCase(); + if (!key || seen[key]) return false; + seen[key] = true; + return true; + }); + } + + // Sort (case-insensitive) + if (SORT_BY) { + rows.sort(function(a, b) { + var A = String(a[SORT_BY] || '').toLowerCase(); + var B = String(b[SORT_BY] || '').toLowerCase(); + if (A < B) return -1; + if (A > B) return 1; + return 0; + }); + } + + g_form.setValue(MRVS_NAME, JSON.stringify(rows)); + } catch (e) { + console.error('MRVS normalise failed', e); + } +} From 70d1039ab9d9b235560c7b8efecfa803eb9954c2 Mon Sep 17 00:00:00 2001 From: hanna-g-sn Date: Tue, 21 Oct 2025 08:58:19 +0100 Subject: [PATCH 3/3] Add function to reset specific MRVS columns --- .../variant_reset_specific_columns.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Client-Side Components/Catalog Client Script/Normalise and Reset a MRVS based on Variable Changes/variant_reset_specific_columns.js diff --git a/Client-Side Components/Catalog Client Script/Normalise and Reset a MRVS based on Variable Changes/variant_reset_specific_columns.js b/Client-Side Components/Catalog Client Script/Normalise and Reset a MRVS based on Variable Changes/variant_reset_specific_columns.js new file mode 100644 index 0000000000..5430d817ae --- /dev/null +++ b/Client-Side Components/Catalog Client Script/Normalise and Reset a MRVS based on Variable Changes/variant_reset_specific_columns.js @@ -0,0 +1,17 @@ +function onChange(control, oldValue, newValue, isLoading) { + if (isLoading) return; + + var MRVS_NAME = 'MY_MRVS'; + var COLUMNS_TO_CLEAR = ['env', 'region']; + + var rows = []; + try { rows = JSON.parse(g_form.getValue(MRVS_NAME) || '[]'); } catch (e) {} + if (!Array.isArray(rows)) rows = []; + + rows.forEach(function(row) { + COLUMNS_TO_CLEAR.forEach(function(col) { if (row.hasOwnProperty(col)) row[col] = ''; }); + }); + + g_form.setValue(MRVS_NAME, JSON.stringify(rows)); +} +