Get Total of currency fields

Samolo Ram Anto
Tera Contributor

Hi, I need to calculate the total of the fields called quantity which are currency fields, and I want a second one that shows the total of all this quantity fields where the sys_id of the task is the same.

I created a BR and this is what i got so far, 

 

 

(function executeRule(current, previous /*null when async*/) {

    var expensetestGR= new GlideRecord('test_expense');

    expensetestGR.addQuery('task', current.sys_id);
    expensetestGR.query();

    var totalAmount = 0;

    while (expensetestGR.next()) {
        totalAmount += parseFloat(current.quantity.getReferenceValue()); 
    }

    current.u_sum = totalAmount;

})(current, previous);

 

 

 

Thank you and best regards.

4 REPLIES 4

Harsh_Deep
Giga Sage
Giga Sage

Hello @Samolo Ram Anto 

 

I'd like to share that it also works in this case of coding, using getAggregate('SUM', 'u_column_name') function.

// EXAMPLE OF CODE

var gr = new GlideAggregate('u_table_name');
gr.addAggregate('SUM', 'u_currency_column');
gr.query();
if(gr.next()) {
    var cost = gr.getAggregate('SUM', 'u_currency_column');
    var total = +total + +cost;
    gs.info('TOTAL COSTS: ' + total + '\n');
}

 

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.

Thanks

Hi, I tried that and I got this.

	// Add your code here
	var expenselineGR = new GlideAggregate('test_expense');
	var currentTask = current.task.getRefRecord();
	expenselineGR.addQuery('task', current.sys_id);
	expenselineGR.addAggregate('SUM', 'quantity');
	expenselineGR.query();
	
	if(expenselineGR.next()){
		currentTask.current.u_sum=expenselineGR.getAggregate('SUM', 'quantity');
		currentTask.update();
	}
	

And it's not working, any idea??

 

Hello @Samolo Ram Anto ,

Replace current.sys_id with currentTask.

Alka_Chaudhary
Mega Sage
Mega Sage

Hello @Samolo Ram Anto ,

Your Script looks correct. I have tried it and it works. Verify the fields backend names that might solve your issue.

If this response clears up your doubt, kindly flag it as both helpful and correct.

Thanks,

Alka