- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2021 08:28 AM
I have this table in an email notification using an email script that is populated via GlideRecord query.
I want to take the “Total Amount” column and add it in the total. I have the “total amount” being populated by multiplying rate (gr.time_sheet.u_adjusted_rate) x hours (gr.total). Total Amount = roundedPay.
I want to just add up all the “roundedPay” and add it in the empty cell. I am not sure how to do this since it is a new variable and not directly from the query. Below is my code (that is not working). Any ideas how I can accomplish this?
var gr=new GlideRecord('time_card');
gr.addQuery('time_sheet', current.sys_id);
gr.addQuery('category','Straight Time');
gr.orderBy('u_date');
gr.query();
var StraightTotal = new GlideAggregate ('time_card');
StraightTotal.addQuery('time_sheet', current.sys_id);
StraightTotal.addQuery('category', 'Straight Time');
StraightTotal.setGroup(false);
StraightTotal.addAggregate('SUM','total');
StraightTotal.query();
//gs.log('Total '+ parseFloat(straightTime).toFixed(2));
if(StraightTotal.next()){
gs.info('SUM: ' + StraightTotal.getAggregate('SUM', 'total'));
}
while(gr.next()){
var straightTime = StraightTotal.getAggregate('SUM', 'total');
var Total = parseFloat(straightTime).toFixed(2);
var rate = gr.time_sheet.u_adjusted_rate;
var hours = gr.total;
var pay = rate * hours;
var roundedPay = parseFloat(pay).toFixed(2);
var TotalAmount = roundedPay;
var obj = JSON.parse(roundedPay);
var nums = [];
var tot = 0;
for (var i=0; i<obj.length; i++){
tot += parseFloat(obj[i]);
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2021 12:08 PM
//This
for (var i=0; i<roundedPay.length; i++){
TotalAmount += roundedPay;
}
//Should be
TotalAmount += roundedPay;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2021 11:20 AM
I made the following adjustment....
var gr=new GlideRecord('time_card');
gr.addQuery('time_sheet', current.sys_id);
gr.addQuery('category','Straight Time');
gr.orderBy('u_date');
gr.query();
var StraightTotal = new GlideAggregate ('time_card');
StraightTotal.addQuery('time_sheet', current.sys_id);
StraightTotal.addQuery('category', 'Straight Time');
StraightTotal.setGroup(false);
StraightTotal.addAggregate('SUM','total');
StraightTotal.query();
//gs.log('Total '+ parseFloat(straightTime).toFixed(2));
if(StraightTotal.next()){
gs.info('SUM: ' + StraightTotal.getAggregate('SUM', 'total'));
}
var TotalAmount = 0;
while(gr.next()){
var straightTime = StraightTotal.getAggregate('SUM', 'total');
var Total = parseFloat(straightTime).toFixed(2);
var rate = gr.time_sheet.u_adjusted_rate;
var hours = gr.total;
var pay = rate * hours;
var roundedPay = parseFloat(pay).toFixed(2);
var obj = JSON.parse(roundedPay);
var nums = [];
var tot = 0;
for (var i=0; i<roundedPay.length; i++){
TotalAmount += roundedPay;
}
gs.info("Rounded: "+ TotalAmount);
I am getting this as my output: "Rounded: 027.4527.4527.4527.4527.4519.9619.9619.9619.9619.96"

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2021 12:01 PM
Get rid of the for loop around that, it should just be the one line doing the math.
Also why are you doing this?
var obj = JSON.parse(roundedPay);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2021 12:07 PM
That was left over from other things I was trying. I have been trying to figure it out for a couple of days!
What exactly should I be getting rid of?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2021 12:08 PM
//This
for (var i=0; i<roundedPay.length; i++){
TotalAmount += roundedPay;
}
//Should be
TotalAmount += roundedPay;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2021 02:23 PM