Get Total of currency fields
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2023 09:24 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2023 10:04 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 05:10 AM
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??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 11:49 AM
Hello @Samolo Ram Anto ,
Replace current.sys_id with currentTask.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2023 10:19 AM
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