How to calculate the sum of the value in multi row variable set?

hongsok
Tera Contributor

Hi,

I am using the MRVS and need to sum the value of the hard drive's size as shown in the pic.

I would appreciate if anyone can give me a clue.

 

find_real_file.png

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

You would have to place a "Total size" variable outside the MRVS for this. Unfortunately though, you would want to calculate that field on the moment you are adding a row. Though, from within the MRVS, you can't influence variables outside the MRVS.

You could think of a workaround. For example, on submission of the Catalog Item, the Total Size will be calculated and the Variable filled. If help needed on this, I can have a look. We have a similar workaround in place. 

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

27 REPLIES 27

hongsok
Tera Contributor

Hi Mark,

I got a result as NaN. I did create a client script to set the size variable to integer.

 

find_real_file.png

Ah oke, Not a Number. Good to see that the basic thought with the Business Rule works, only the calculation not there yet.

Could you share how the script looks like now? And are you inputting just whole numbers, or decimals? (because my script was about decimals)

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

hongsok
Tera Contributor

Hi Mark,

Here is the script.

The field that I am going to sum is size and the result field name is total_disk_size.

 

(function executeRule(current, previous /*null when async*/) {

var grMRVSAnswer = new GlideRecord('sc_multi_row_question_answer');
grMRVSAnswer.addSizeQuery('parent_id=' + current.getUniqueValue() + '^item_option_new=b9092292dbf733402614f81d0f961901');
grMRVSAnswer._query();

var totalInt = 0;
while(grMRVSAnswer._next()) {
if(grMRVSAnswer.value != '') {
// var amountInt = grMRVSAnswer.value.replace(',', '.');

totalInt = parseFloat(totalInt) + parseFloat(amountInt);
}
}

current.variables.total_disk_size = totalInt.toFixed(2).replace('.', ',');
current.update();


})(current, previous);

I have to test, though you could already change:

totalInt = parseFloat(totalInt) + parseFloat(amountInt);

into:

totalInt = parseInt(totalInt) + parseInt(amountInt);

And change:

current.variables.total_disk_size = totalInt.toFixed(2).replace('.', ',');

into:

current.variables.total_disk_size = totalInt;

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Maybe also add some logging, after the while a gs.info for example.

while(grMRVSAnswer._next()) {
gs.info('--> Value: ' + grMRVSAnswer.value);
if(grMRVSAnswer.value != '') {

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn