GlideRecord the sums fields value of multiple records
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-22-2018 11:24 AM
We have built an app called recognition toolkit. Toolkits consist of gift cards that are issued to managers who then gift the cards to employees for good work. What I want to do is after any card is issued from a toolkit I want the script to loop thru the toolkits inventory, summing the card_count field. If they all equal 0 then I will automatically close the toolkit and notify HR.
I'm already doing something similar. When a toolkit is recalled for any reason the following script runs closes the toolkit and puts all of the remaining toolkit inventory back into the main inventory. Works great. I just can't figure out how to loop thru the card counts keeping a running total. Any help would be appreciated
current.state = 500;
current.closed_at = gs.nowDateTime();
current.closed_by = gs.getUserID();
current.update();
gs.addInfoMessage('Toolkit Recalled');
action.setRedirectURL(current);
var mtc = current.sys_id;
var gr = new GlideRecord('u_manager_toolkit_inventory');
gr.addQuery('u_mtc_number', mtc);
gr.query();
while(gr.next()){
var current_cnt = gr.u_card_count;
var card_type = gr.u_card_type;
//Add card count back to card type inventory
var grCT = new GlideRecord('u_card_type');
grCT.addQuery('sys_id', card_type);
grCT.query();
if(grCT.next()){
grCT.u_card_count = grCT.u_card_count + current_cnt;
grCT.update();
}
gr.u_card_count = 0;
gr.update();
}
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-22-2018 11:32 AM
Here's an example BR script (on the child record table) I use to add up hours to a parent in a custom table I have related to Project. You can probably follow what it's doing to build your own.
(function executeRule(current, previous /*null when async*/) {
var prj = new GlideRecord('u_project_scope_change_request');
prj.addQuery('sys_id', current.u_parent);
prj.query();
while(prj.next()){
var tc = new GlideRecord('u_project_scope_change_financials');
tc.addQuery("u_parent", prj.sys_id);
var fy1Sum = 0;
var fy2Sum = 0;
tc.query();
while (tc.next()) {
fy1Sum += tc.u_total_capex;
fy2Sum += tc.u_total_opex;
}
prj.u_total_capex = fy1Sum;
prj.u_total_opex = fy2Sum;
prj.update();
}
})(current, previous);