How to sum field total value from list of records and copy that result sum into another table field

Kishore8
Kilo Guru

Here my script:

but not working it..

Business rule: table :incident

var totalA = 0;

  totalA += current.u_used_amount;

var bRec = new GlideRecord('u-details');

bRec.query();

if(bRec.next()){

bRec.u_reaming_balance = totalA;

bRec.update();

}

find_real_file.png

8000+5000+3000=16000 , display result sum in u_details table

Table: u_details

Field: u_remaing_balance

1 ACCEPTED SOLUTION

Hi Srivastav,




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


var cal = 0;


var gr = new GlideRecord('incident');


gr.addQuery('category', 'software');     // is it mandatory ??


gr.query();


while(gr.next())


cal = parseInt(cal) + parseInt(gr.u_used_amount);



var bRec = new GlideRecord('u_details');


bRec.addQuery('fieldname', 'pass the filter value here'); //if   it is new table of field then the field is empty , WHAT   DO NOW    


bRec.query();,


if(bRec.next()){


bRec.u_reaming_balance = cal;


bRec.update();


}


})(current, previous);


View solution in original post

15 REPLIES 15

Shishir Srivast
Mega Sage

You need to do GlideRecord on incident table as well to get the used amount for all the record and then sum them up and then pass to another table u_details.


Shishir Srivast
Mega Sage

Example:



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


var cal = 0;


var gr = new GlideRecord('incident');


gr.addQuery('category', 'software');


gr.query();


while(gr.next())


cal = parseInt(cal) + parseInt(gr.u_used_amount);



var bRec = new GlideRecord('u_details');


bRec.addQuery('fieldname', 'pass the filter value here');


bRec.query();


if(bRec.next()){


bRec.u_reaming_balance = cal;


bRec.update();


}


})(current, previous);


Hi Srivastav,




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


var cal = 0;


var gr = new GlideRecord('incident');


gr.addQuery('category', 'software');     // is it mandatory ??


gr.query();


while(gr.next())


cal = parseInt(cal) + parseInt(gr.u_used_amount);



var bRec = new GlideRecord('u_details');


bRec.addQuery('fieldname', 'pass the filter value here'); //if   it is new table of field then the field is empty , WHAT   DO NOW    


bRec.query();,


if(bRec.next()){


bRec.u_reaming_balance = cal;


bRec.update();


}


})(current, previous);


gr.addQuery('category', 'software');     // is it mandatory ??


is not mandatory, but if you do not provide the filter then it will query the whole incident record to calculate the value. take appropriate action as per the business requirement, if you want to calculate for all the incident records then you do not need to provide filter else please consider to include.



bRec.addQuery('fieldname', 'pass the filter value here'); //if   it is new table of field then the field is empty , WHAT   DO NOW  


field will be empty on the record not on the table, I hope you will be passing the calculated value of any particular record, how will you get the record to set the value if you fo filer based upon the sys_id or any other parameter?