
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2018 12:45 PM
I have the below script the is giving me the total time charge to a particular task per record. Can someone help me in getting the script to add all of the different totals into one combined total?For example the below screen shot, shows 127.0 and 20.0. I would like the script to print out 147 which is the total combined.
(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('time_card');
gr.addQuery('u_project', current.sys_id);
gr.query();
while (gr.next()) {
var total = gr.sunday + gr.monday + gr.tuesday + gr.wednesday + gr.thursday + gr.friday + gr.saturday;
gs.addInfoMessage(total);
//gr.setWorkflow(false);
//gr.update();
}
})(current, previous);
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2018 12:58 PM
You need to move total outside the loop:
var total = 0;
var gr = new GlideRecord('time_card');
gr.addQuery('u_project', current.sys_id);
gr.query();
while (gr.next()) {
total = total + gr.sunday + gr.monday + gr.tuesday + gr.wednesday + gr.thursday + gr.friday + gr.saturday;
gs.addInfoMessage(total);
//gr.setWorkflow(false);
//gr.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2018 01:20 PM
Since you want it to only print out one result, you should move the gs.addInfoMessage(total) outside of the while loop. This should do what you want:
var total = 0;
var gr = new GlideRecord('time_card');
gr.addQuery('u_project', current.sys_id);
gr.query();
while (gr.next()) {
total = total + gr.sunday + gr.monday + gr.tuesday + gr.wednesday + gr.thursday + gr.friday + gr.saturday;
}
gs.addInfoMessage(total);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2018 01:30 PM
Dear Edwin,
Just made an after business rule on time_card to achieve what you are look for as an info message on the time card when someone submits it. This should work from project table as well once you update the query variable.
script :
(function executeRule(current, previous /*null when async*/) {
var query = 'category=task_work'; // need to update this to have the correct query, I believe it is 'u_project=' + current.sys_id
var total = 0; // as mentioned by @Michael Ritchie
var grTc = new GlideRecord('time_card');
grTc.addEncodedQuery(query);
grTc.query();
while(grTc.next()) {
total+= grTc.sunday + grTc.monday + grTc.tuesday +
grTc.wednesday + grTc.thursday + grTc.friday +
grTc.saturday;
}
gs.addInfoMessage("Total Hrs so far = " + total);
})(current, previous);
Output :