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 changed it to TotalAmount += parseFloat(roundedPay); and now it is givine me to the total. Thank you so much for the help!!! This has been driving me crazy. 

Ah, I was not thinking, you did a "toFixed" which returns a string, not a number.

 

Hello 

Could you please share the Mail script ,How did u created this table and added values ,We have same requirement

 

Thanks In Advance

Cherry,

Here is the whole script for the table. I hope this helps! 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {
	template.print('<center>');
	template.print('<table id="table">');	
	template.print( "<tr>" );
	template.print( "<td style=\"border: 1px solid; border-color: black; padding: 5px;\" ><left><b>Day</b></left></td>" );
	template.print( "<td style=\"border: 1px solid; border-color: black; padding: 5px;\" ><left><b>Date Worked</b></left></td>" );
	template.print( "<td style=\"border: 1px solid; border-collapse: collapse; border-color: black;\" ><left><b>Description of Work</b></left></td>" );
	template.print( "<td style=\"border: 1px solid; border-color: black;padding: 5px;\" ><left><b>Rate of Pay</b></left></td>" );
	template.print( "<td style=\"border: 1px solid; border-color: black;padding: 5px;\" ><left><b>Hours Worked</b></left></td>" );
	template.print( "<td style=\"border: 1px solid; border-color: black;padding: 5px;\" ><left><b>Total Amount</b></left></td>" );
	template.print( "</tr>" );
	template.print( "</td>" );

	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);
		
		TotalAmount += parseFloat(roundedPay);
		//	gs.info("Total Amount: "+ TotalAmount);

template.print("<td style=\"border: 1px solid; border-color: black;padding: 5px;\" ><center>" + gr.u_day.getDisplayValue() + "</center></td>" );
template.space(4);
template.print("<td style=\"white-space: nowrap;border: 1px solid; border-color: black;padding: 5px;\" ><center>" + gr.u_date + "</center></td>" );
template.space(4);
template.print("<td style=\"border: 1px solid; border-color: black;padding: 5px;\" ><center>" +gr.comments +"</center></td>" );
template.space(4);
template.print("<td style=\"border: 1px solid; border-color: black;padding: 5px;\" ><center>" +gr.time_sheet.u_adjusted_rate +"</center></td>" );
template.space(4);
template.print("<td style=\"border: 1px solid; border-color: black;\"><center>"+gr.total+"</center></td>");	
template.space(4);
template.print("<td style=\"border: 1px solid; border-color: black;padding: 5px; id: countme; \"><center>$"+roundedPay+"</center></td>" );
template.space(4);
template.print('<br>');
template.print( "</tr>" );
template.print( "</td>" );
}

template.print("<td style=\"border: 0px solid; border-color: white; padding: 0px;\" ><center>"+"</center></td>" );
template.space(4);
template.print("<td style=\"border: 0px solid;border-color: white;padding: 0px;\" ><center>" +  "</center></td>" );
template.space(4);
template.print("<td style=\"border: 0px solid;border-color: white;padding: 0px;\" ><center>" +  "</center></td>" );
template.space(4);
template.print("<td style=\"border: 1px solid; border-color: white;padding: 5px;\" ><center><b>" +'Totals' +"<b></center></td>" );
template.space(4);
template.print("<td style=\"border: 1px solid; border-color: black;padding: 5px;\"><center>"+straightTime+"</center></td>" );
template.space(4);
template.print("<td style=\"border: 1px solid; border-color: black;\"><center>$"+TotalAmount+"</center></td>");	
template.space(4);
template.print('<br>');
template.print( "</tr>" );
	template.print('</table>');
	template.print('<span id="val"></span>');
	template.print('</center>');
		
		})(current, template, email, email_action, event);