Script to calculate numeric value

sukran
Mega Sage

Script query return 3 numeric value for one record size value

Requirement is that , we need to calculate (add) all these and update the total through BR

 

sukran_0-1721295477878.png

 

2 ACCEPTED SOLUTIONS

hi @sukran 

Please try the below code its working for me i have tried the same in PDI .

var arr = [];
var arr1 = [];
var totalSum=0;
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('sys_id', '79f66bd31b6dc8147fcc202a2d4bcbae');
gr.query();
while (gr.next()) {
    var compsys = gr.sys_id;
    var gr1 = new GlideRecord('cmdb_ci_disk');
    gr1.addQuery('computer', compsys);
    gr1.query();
    while (gr1.next()) {
        var siz = gr1.size;
        var fsiz = parseInt(siz);
        arr.push(fsiz);
        gs.info(arr);
    }
    var val = arr.split(",");
    for (i = 0; i < arr.length; i++){
        totalSum += arr[i];
       
    }
   gs.info(totalSum);
}

 

Please try to run it in background first.

 

Thank you!

Vishakha Yadav

View solution in original post

Thanks ,. this information helps and now i understand what you are struggling with.

Here is a script that will work

var totalSize = 0;
var gr = new GlideRecord("cmdb_ci_disk");
gr.addEncodedQuery("computer=5f9b83bfc0a8010e005a2b3212c9dc07"); //use sys_id of computer you want to add
gr.query();
while (gr.next()) {
   gs.print(gr.size);
   var s = parseFloat(gr.size.toString().replace(/GB/g, ""));
   totalSize = totalSize + s
}
gs.print(totalSize);
-Anurag

View solution in original post

12 REPLIES 12

@Jim Coyne its returning result 655

Well that's odd.  Getting 665 here, but the point is that is incorrect.

 

What unit would you want the results to be in?  Do you want to see all the results in TB, GB or MB?  Because you need to know what the input unit is before you can add them properly.  And then output them in a standard unit?

vishakhayadav24
Tera Guru

HI @sukran 

You can use below code

var f1=parseInt(val1);

var f2=parseInt(val2);

var f3=parseInt(val3);

var final=f1+f2+f3;