Calculating totals from mult-row variable set rows in to a variable on a record producer?

Community Alums
Not applicable

Is there a way to calculate a total from values in rows in a multi-row variable set (mrvs) into a variable outside of the mrvs on a catalog item?

Use-case that I have is a user is entering in various tuition expenses in each row of the mrvs, and those entries are totaled in the last column of that row entry (e.g. books + fees + tuition = total cost). They can enter this information in for multiple classes via the mrvs. As they are entering in the various rows I'd like to update a 'grand total' field on the record producer that is the sum of the 'total cost' in each row of the mrvs.

find_real_file.png

The Docs site states: "Scripts that are not included in a multi-row variable set cannot affect variables inside the multi-row variable set. Similarly, the scripts included in the multi-row variable set cannot affect the variables that are not included in the multi-row variable set."

I'm just curious if anyone else has found a way to make this work or if I need to do away with the mrvs and use variables on the record producer. 

Thanks

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you will have to write onSubmit client script and parse the json and set the value by summing up the values

Sample script below

function onSubmit(){

var value = g_form.getValue('mrvs_variable name'); // your MRVS variable name here

var parser = JSON.parse(value);

var sum = 0;

for(var i=0;i<parser.length;i++){

sum = sum + parseFloat(parser[i].estimated_total_cost); // give here variable name inside MRVS

}

g_form.setValue('course_subtotal', sum); // give here the proper variable name here

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you will have to write onSubmit client script and parse the json and set the value by summing up the values

Sample script below

function onSubmit(){

var value = g_form.getValue('mrvs_variable name'); // your MRVS variable name here

var parser = JSON.parse(value);

var sum = 0;

for(var i=0;i<parser.length;i++){

sum = sum + parseFloat(parser[i].estimated_total_cost); // give here variable name inside MRVS

}

g_form.setValue('course_subtotal', sum); // give here the proper variable name here

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

This seems to work, thank you for your assistance!

You are welcome.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

What if you need the value to change instantly visually for the user?  Would writing the script as an onChange script do it?  How would you write it that way?  Thanks.