|
1 | | -(function executeRule(current, previous /*null when async*/) { |
2 | | - |
| 1 | +(function executeRule(current, previous /*null when async*/ ) { |
3 | 2 | // --- Configuration --- |
4 | | - var VARIABLE_NAME_TO_POPULATE_WITH_SUM = 'total_estimate'; // Add the name of your variable where total should be saved |
5 | | - var MRVS_INTERNAL_NAME = 'item_details'; // Add the internal name of your multi-row variable set |
6 | | - var MRVS_VARIABLE_NAME_TO_SUM = 'quoted_price'; // Add the variable name that contains the value to be added for each row |
| 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; |
7 | 9 |
|
8 | | - // -- Don't change below -- |
| 10 | + // Get the MRVS object |
9 | 11 | var mrvs = current.variables[MRVS_INTERNAL_NAME]; |
10 | 12 |
|
11 | | - var total_value = 0; |
12 | | - |
13 | | - // The mrvs variable is already a JSON string, so we need to parse it |
14 | | - var mrvsRows = JSON.parse(mrvs); |
| 13 | + // Get the number of rows |
| 14 | + var rowCount = mrvs.getRowCount(); |
15 | 15 |
|
16 | 16 | // Loop through the parsed array of rows |
17 | | - for (var i = 0; i < mrvsRows.length; i++) { |
18 | | - var row = mrvsRows[i]; |
19 | | - var line_price = parseFloat(row[MRVS_VARIABLE_NAME_TO_SUM]) || 0; |
20 | | - total_value += line_price; |
| 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 | 21 | } |
22 | 22 |
|
23 | | - |
24 | 23 | current.variables[VARIABLE_NAME_TO_POPULATE_WITH_SUM] = total_value.toFixed(2); |
25 | 24 |
|
26 | | - |
27 | 25 | })(current, previous); |
0 commit comments