Skip to content

Commit 4f2f200

Browse files
authored
feat(business rule) Add Business Rule to calculate MRVS total (#2024)
* Create readme.md * Create mrvs_total_sum.js * Update mrvs_total_sum.js to use getRow method
1 parent 2ccaff9 commit 4f2f200

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(function executeRule(current, previous /*null when async*/ ) {
2+
// --- Configuration ---
3+
var VARIABLE_NAME_TO_POPULATE_WITH_SUM = 'total_estimate'; // Variable to store the total
4+
var MRVS_INTERNAL_NAME = 'item_details'; // Internal name of your multi-row variable set
5+
var MRVS_VARIABLE_NAME_TO_SUM = 'quoted_price'; // Variable name containing the value to sum
6+
7+
// --- Don't change below ---
8+
var total_value = 0;
9+
10+
// Get the MRVS object
11+
var mrvs = current.variables[MRVS_INTERNAL_NAME];
12+
13+
// Get the number of rows
14+
var rowCount = mrvs.getRowCount();
15+
16+
// Loop through the parsed array of rows
17+
for (var i = 0; i < rowCount; i++) {
18+
var row = mrvs.getRow(i);
19+
var line_price = parseFloat(row[MRVS_VARIABLE_NAME_TO_SUM]) || 0;
20+
total_value += line_price;
21+
}
22+
23+
current.variables[VARIABLE_NAME_TO_POPULATE_WITH_SUM] = total_value.toFixed(2);
24+
25+
})(current, previous);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Server-Side MRVS Total Calculator
2+
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.
3+
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.
4+
5+
## How to Implement
6+
Follow these steps to configure the Business Rule in your instance.
7+
8+
### 1. Prerequisites
9+
Before creating the rule, make sure you have the following variables on your Catalog Item:
10+
11+
- A Multi-Row Variable Set (e.g., named item_details).
12+
- A variable inside the MRVS that will contain a number (e.g., named quoted_price).
13+
- A single variable outside the MRVS to store the final sum (e.g., a Single Line Text variable named total_estimate).
14+
15+
### 2. Business Rule Configuration
16+
Create a new Business Rule with the following settings:
17+
- Name: A descriptive name like Calculate MRVS Total on RITM.
18+
- Table: Requested Item [sc_req_item].
19+
- Advanced: Check this box to reveal the script field.
20+
- When to run:
21+
- When: Before
22+
- Insert: true
23+
- Update: true
24+
25+
Copy and paste the script from `mrvs_total_sum.js` into the Script field within the Advanced tab of your Business Rule.
26+
27+
Before saving, you must update the three configuration variables at the top of the script to match your specific setup.
28+
You will need to set the following:
29+
- `VARIABLE_NAME_TO_POPULATE_WITH_SUM` to the internal name of your total variable
30+
- `MRVS_INTERNAL_NAME` to the internal name of your Multi-Row Variable Set
31+
- `MRVS_VARIABLE_NAME_TO_SUM` to the internal name of the numeric variable inside the MRVS that you want to sum.
32+

0 commit comments

Comments
 (0)