- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2020 02:21 PM
Hi,
I'm using a fairly simple Business Rule to try to get the sum of a field called u_total_cost on RITM and then update its parent sc_request. It all works...EXCEPT it is taking the last value - NOT the SUM.
I'm running this as an After Business Rule that runs on Insert and Update. It is also updating the Total Cost field on the RITM.
Here is the script:
(function executeRule(current, previous /*null when async*/) {
//Update Request Item Total Cost
var thisRecord = current.sys_id;
var gr = new GlideRecord ('sc_req_item');
gr.addQuery('sys_id',"=",thisRecord);
gr.query();
if (gr.next()) {
var a = current.price;
var b = current.quantity;
current.u_total_cost= a*b;
current.update();
}
//Update parent Request
var reqID = current.request.sys_id;
var agg = new GlideAggregate('sc_req_item');
agg.addQuery('request', reqID);
agg.addAggregate('SUM', 'u_total_cost');
agg.query();
var cost = 0;
while (agg.next()) {
cost = agg.getAggregate('SUM', 'u_total_cost');
var req = new GlideRecord ('sc_request');
req.addQuery('sys_id',reqID);
req.query();
if (req.next()); {
req.u_purchase_cost = cost;
req.update();
}
}
})(current, previous);
Can anyone tell me why this isn't summing?
Thank you,
Robert
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2020 04:22 PM
Sorry, I didn't read your question properly.
Documentation and examples are a bit sparse but checking quickly in a dev instance
I found similar results to yours, until I added agg.setGroup(false); into my query.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2020 05:05 PM
Below is what SN has in docs so try to do something similar.
var totalCostSum = new GlideAggregate('fixed_asset');
totalCostSum.addAggregate('SUM', 'total_cost');
totalCostSum.query();
while (totalCostSum.next()) {
var allTotalCost = 0;
allTotalCost = totalCostSum.getAggregate('SUM', 'total_cost');
aTotalCost = totalCostSum.getValue('total_cost');
gs.print('Unique field value: ' + aTotalCost + ', SUM = ' + allTotalCost + ', ' + allTotalCost/aTotalCost + ' records');
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2020 05:10 PM
Also try
(function executeRule(current, previous /*null when async*/) {
var a = current.price;
var b = current.quantity;
current.u_total_cost= a*b;
current.update();
//Update parent Request
var reqID = current.request.sys_id;
var agg = new GlideAggregate('sc_req_item');
agg.addQuery('request', reqID);
agg.addAggregate('SUM', 'u_total_cost');
agg.groupBy('u_total_cost');
agg.query();
var cost = 0;
while (agg.next()) {
cost = agg.getAggregate('SUM', 'u_total_cost');
var req = new GlideRecord ('sc_request');
req.addQuery('sys_id', reqID);
req.query();
if (req.next()){
req.u_purchase_cost = cost;
req.update();
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2020 07:20 PM
I think you do not need to GlideRecord sc_req_item table , You can just use below codes to update total_cost on ritm table.
var a = current.price;
var b = current.quantity;
current.u_total_cost= a*b;
current.update();
I believe issue is happening because you are using current.update() in between the business rule which could again be trigerring same business rule.
I would advice to create a separate before business rule to update u_total_cost on RITM table. Please do not use current.update() on before business rule , It is not required and cause issues.
You can leave the remaining code in this business rule and I think it should work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2020 07:42 AM
Hi Mike, this is actually what I did with the input from Tony above. Had I not already been in that thread, I would have done this and marked this correct.
Thank you!
Robert

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2020 07:46 AM