Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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));
}

Loading