From 80749436340c98daf1cccab620c91541cff1494a Mon Sep 17 00:00:00 2001 From: JohanDC-1999 <82451675+JohanDC-1999@users.noreply.github.com> Date: Sat, 11 Oct 2025 11:47:55 +0200 Subject: [PATCH 1/3] Create readme.md --- .../Business Rules/Find MRVS Total/readme.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Server-Side Components/Business Rules/Find MRVS Total/readme.md diff --git a/Server-Side Components/Business Rules/Find MRVS Total/readme.md b/Server-Side Components/Business Rules/Find MRVS Total/readme.md new file mode 100644 index 0000000000..7fc6dd9ecc --- /dev/null +++ b/Server-Side Components/Business Rules/Find MRVS Total/readme.md @@ -0,0 +1,32 @@ +# Server-Side MRVS Total Calculator +This ServiceNow Business Rule automatically calculates the sum of a numeric field from all rows within a Multi-Row Variable Set (MRVS) after a catalog item has been submitted. +It populates a separate variable with the calculated total, making the value easily accessible for flows, reports, and integrations without needing to parse the MRVS JSON every time. This script is designed to run on the back-end, ensuring the total is accurate and persistent. + +## How to Implement +Follow these steps to configure the Business Rule in your instance. + +### 1. Prerequisites +Before creating the rule, make sure you have the following variables on your Catalog Item: + +- A Multi-Row Variable Set (e.g., named item_details). +- A variable inside the MRVS that will contain a number (e.g., named quoted_price). +- A single variable outside the MRVS to store the final sum (e.g., a Single Line Text variable named total_estimate). + +### 2. Business Rule Configuration +Create a new Business Rule with the following settings: +- Name: A descriptive name like Calculate MRVS Total on RITM. +- Table: Requested Item [sc_req_item]. +- Advanced: Check this box to reveal the script field. +- When to run: + - When: Before + - Insert: true + - Update: true + +Copy and paste the script from `mrvs_total_sum.js` into the Script field within the Advanced tab of your Business Rule. + +Before saving, you must update the three configuration variables at the top of the script to match your specific setup. +You will need to set the following: +- `VARIABLE_NAME_TO_POPULATE_WITH_SUM` to the internal name of your total variable +- `MRVS_INTERNAL_NAME` to the internal name of your Multi-Row Variable Set +- `MRVS_VARIABLE_NAME_TO_SUM` to the internal name of the numeric variable inside the MRVS that you want to sum. + From 32a9954679bf548e397ed36e22eac90c31762aa8 Mon Sep 17 00:00:00 2001 From: JohanDC-1999 <82451675+JohanDC-1999@users.noreply.github.com> Date: Sat, 11 Oct 2025 11:51:45 +0200 Subject: [PATCH 2/3] Create mrvs_total_sum.js --- .../Find MRVS Total/mrvs_total_sum.js | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Server-Side Components/Business Rules/Find MRVS Total/mrvs_total_sum.js diff --git a/Server-Side Components/Business Rules/Find MRVS Total/mrvs_total_sum.js b/Server-Side Components/Business Rules/Find MRVS Total/mrvs_total_sum.js new file mode 100644 index 0000000000..c9c11821d0 --- /dev/null +++ b/Server-Side Components/Business Rules/Find MRVS Total/mrvs_total_sum.js @@ -0,0 +1,27 @@ +(function executeRule(current, previous /*null when async*/) { + + // --- Configuration --- + var VARIABLE_NAME_TO_POPULATE_WITH_SUM = 'total_estimate'; // Add the name of your variable where total should be saved + var MRVS_INTERNAL_NAME = 'item_details'; // Add the internal name of your multi-row variable set + var MRVS_VARIABLE_NAME_TO_SUM = 'quoted_price'; // Add the variable name that contains the value to be added for each row + + // -- Don't change below -- + var mrvs = current.variables[MRVS_INTERNAL_NAME]; + + var total_value = 0; + + // The mrvs variable is already a JSON string, so we need to parse it + var mrvsRows = JSON.parse(mrvs); + + // Loop through the parsed array of rows + for (var i = 0; i < mrvsRows.length; i++) { + var row = mrvsRows[i]; + var line_price = parseFloat(row[MRVS_VARIABLE_NAME_TO_SUM]) || 0; + total_value += line_price; + } + + + current.variables[VARIABLE_NAME_TO_POPULATE_WITH_SUM] = total_value.toFixed(2); + + +})(current, previous); From 9e0ec0c7a689dd3e5357ba60096d8bea910563d5 Mon Sep 17 00:00:00 2001 From: JohanDC-1999 <82451675+JohanDC-1999@users.noreply.github.com> Date: Sat, 11 Oct 2025 15:56:15 +0200 Subject: [PATCH 3/3] Update mrvs_total_sum.js to use getRow method --- .../Find MRVS Total/mrvs_total_sum.js | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/Server-Side Components/Business Rules/Find MRVS Total/mrvs_total_sum.js b/Server-Side Components/Business Rules/Find MRVS Total/mrvs_total_sum.js index c9c11821d0..c84ec3223f 100644 --- a/Server-Side Components/Business Rules/Find MRVS Total/mrvs_total_sum.js +++ b/Server-Side Components/Business Rules/Find MRVS Total/mrvs_total_sum.js @@ -1,27 +1,25 @@ -(function executeRule(current, previous /*null when async*/) { - +(function executeRule(current, previous /*null when async*/ ) { // --- Configuration --- - var VARIABLE_NAME_TO_POPULATE_WITH_SUM = 'total_estimate'; // Add the name of your variable where total should be saved - var MRVS_INTERNAL_NAME = 'item_details'; // Add the internal name of your multi-row variable set - var MRVS_VARIABLE_NAME_TO_SUM = 'quoted_price'; // Add the variable name that contains the value to be added for each row + var VARIABLE_NAME_TO_POPULATE_WITH_SUM = 'total_estimate'; // Variable to store the total + var MRVS_INTERNAL_NAME = 'item_details'; // Internal name of your multi-row variable set + var MRVS_VARIABLE_NAME_TO_SUM = 'quoted_price'; // Variable name containing the value to sum + + // --- Don't change below --- + var total_value = 0; - // -- Don't change below -- + // Get the MRVS object var mrvs = current.variables[MRVS_INTERNAL_NAME]; - var total_value = 0; - - // The mrvs variable is already a JSON string, so we need to parse it - var mrvsRows = JSON.parse(mrvs); + // Get the number of rows + var rowCount = mrvs.getRowCount(); // Loop through the parsed array of rows - for (var i = 0; i < mrvsRows.length; i++) { - var row = mrvsRows[i]; - var line_price = parseFloat(row[MRVS_VARIABLE_NAME_TO_SUM]) || 0; - total_value += line_price; + for (var i = 0; i < rowCount; i++) { + var row = mrvs.getRow(i); + var line_price = parseFloat(row[MRVS_VARIABLE_NAME_TO_SUM]) || 0; + total_value += line_price; } - current.variables[VARIABLE_NAME_TO_POPULATE_WITH_SUM] = total_value.toFixed(2); - })(current, previous);