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

is u_total_budget a SUM of all the second field from incident table? if   yes, let's see if this helps.



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



// Add your code here


var cal = 0;


var gr = new GlideRecord('incident');


gr.addQuery('u_portfolio', current.u_portfolio);


gr.query();


while(gr.next())


cal = parseFloat(cal) + parseFloat(gr.getValue('u_second').substring(4).replace(',', ''));



var bRec = new GlideRecord('u_sales_lead');


if(bRec.get('sys_id', current.u_portfolio)){


bRec.u_reaming_budget = parseFloat(cal) - parseFloat(current.u_total_budget.substring(4).replace(',', ''));


bRec.update();


}


})(current, previous);


Hi Shishir,



I am waiting for your reply



gr.addQuery('u_portfolio', current.u_portfolio);


Not working...





Here we have relation between incident and sales tables



If you see the sales table- there are 2 records are there


1.cisco


2.unisco



Sales table is reference table to portfolio field in incident.


So based on portfolio value calculate total sum from list of records and get that value , subtract total sum value from


Sales table from related recods




Ex: sales table have 2-records


1.cisco


2. Unisco



In incident table


Portfolio :Cisco       related   total records 4, get total 4 records sum value and subtraction from sales table field- Total Budget



As the same way



Portfolio: unisco related total records 5



Note: if you want more details I will explain.... please


Hi Shisir,



As you suggested , i have mentioned your script   , but not working.



var cal = 0;


var gr = new GlideRecord('incident');


gr.addQuery('u_portfolio', current.u_portfolio);


gr.query();


while(gr.next())


cal = parseFloat(cal) + parseFloat(gr.getValue('u_second').substring(4).replace(',', ''));



var bRec = new GlideRecord('u_sales');


if(bRec.get('sys_id', current.u_portfolio)){


bRec.u_remaing_fund = parseFloat(cal) - parseFloat(current.u_total_fund.substring(4).replace(',', ''));


bRec.update();


}





find_real_file.png


find_real_file.png



find_real_file.png


Hi again,




i hope it will makes understand the query...


find_real_file.png


Hi,

Can you please let me know how and where to use these scripts.

Completely new to scripting in servicenow and I have same requirement as above.

 

Thanks in Advance