How to sum variables from a loop?

Lisa Silvaroli
Tera Guru

I have this table in an email notification using an email script that is populated via GlideRecord query.
find_real_file.png

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]);
		}
1 ACCEPTED SOLUTION

//This
		for (var i=0; i<roundedPay.length; i++){
			TotalAmount += roundedPay;
		}


//Should be
		TotalAmount += roundedPay;

View solution in original post

13 REPLIES 13

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"

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);

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? 

//This
		for (var i=0; i<roundedPay.length; i++){
			TotalAmount += roundedPay;
		}


//Should be
		TotalAmount += roundedPay;

That is just putting them together but not actually adding them . 

 

Result:

find_real_file.png