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

Hi,

you can try creating onChange on that variable within MRVS

check these links on how to access the variables outside the MRVS inside your script

https://community.servicenow.com/community?id=community_question&sys_id=84c17148db791c5014d6fb24399619bc

https://community.servicenow.com/community?id=community_question&sys_id=def61f32dbf5dc102e8c2183ca961951

https://community.servicenow.com/community?id=community_question&sys_id=a3d51cb7dbe8ac10a08a1ea66896199f

Regards
Ankur

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

Hi Ankur,

I'm not sure why this is not working for me. 

I have 2 fields in MRVS, the name of the item and the amount to be deducted.

I used your code, however, the Total Deduction Amount amount field is not getting populated by the sum of the Deduction Amount field

find_real_file.png

 

Here is my script 

function onSubmit() {
   //Type appropriate comment here, and begin script below
var value = g_form.getValue('u_deduction_amount'); // 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].u_deduction_amount); // give here variable name inside MRVS
}

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

}

 

Your help on this will be much appreciated. Thank you in advance.

Hi,

did you alert what came in the sum?

Also is the mrvs variable name correct for u_decuction_amount

Seems the name of MRVS variable set is also not correct?

function onSubmit() {
   //Type appropriate comment here, and begin script below
var value = g_form.getValue('u_deduction_amount'); // MRVS variable set name here
var parser = JSON.parse(value);
var sum = 0;

for (var i = 0; i < parser.length; i++) {
    sum = sum + parseFloat(parser[i].u_deduction_amount); // give here variable name inside MRVS
}

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

}

Regards
Ankur

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

Hi Ankur,
Can we also do it for SRVS ??

Hi @Ankur Bawiskar,

 

Is there any way to auto-populate the total amount field?

 

Thanks!