- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2025 01:10 AM - edited 06-27-2025 01:10 AM
Hi All,
I am getting the count of string column (u_new_call_amount) for records(155) in the table and then need to create a SINGLE record in another custom table for same 'Supplier' (792d9fc82b956690f90cf760ce91bf88) and set the total call count and total sum count as per below given result
Note: I don't want to create 155 records in another table that is only ask i have, only one record needs to be created
*** Script: Total calls are: 155
*** Script: Sum of all calls are: 11267
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2025 10:59 PM
are you using correct table, field?
use COUNT and SUM correctly
u_new_call_amount -> should be integer and not string
Ensure no empty data is there
try this
var supplierId = '792d9fc82b956690f90cf760ce91bf88';
// Use the correct table name
var ga = new GlideAggregate('u_hp_payfile');
ga.addQuery('u_supplier', supplierId);
ga.addNotNullQuery('u_new_call_amount');
ga.addAggregate('COUNT'); // Count all records
ga.addAggregate('SUM', 'u_new_call_amount'); // Sum of amounts
ga.query();
var totalCalls = 0;
var totalSum = 0;
if (ga.next()) {
// COUNT returns the number of records
totalCalls = parseInt(ga.getAggregate('COUNT'));
// SUM may return null/empty if the field is not numeric
var sumValue = ga.getAggregate('SUM', 'u_new_call_amount');
totalSum = sumValue ? parseInt(sumValue) : 0;
}
gs.info('Total calls: ' + totalCalls);
gs.info('Total sum: ' + totalSum);
// Create/update summary record in u_payfile_summary
var grSummary = new GlideRecord('u_payfile_summary');
grSummary.addQuery('u_partner_name', supplierId);
grSummary.query();
if (grSummary.next()) {
// Update existing record
grSummary.u_total_amount_payable = totalSum;
grSummary.u_total_no_of_calls = totalCalls;
grSummary.update();
} else {
// Insert new record
grSummary.initialize();
grSummary.u_partner_name = supplierId;
grSummary.u_total_amount_payable = totalSum;
grSummary.u_total_no_of_calls = totalCalls;
grSummary.insert();
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2025 07:52 AM
Hi @Ankur Bawiskar thanks for the response, i am executing your code just to print to get total calls and total sum but i am getting below result only 1 call and its sum however i have 155 calls / record and the sum is 11267 its not matching with your result
*** Script: Total calls: 1
*** Script: Total sum: 828
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2025 07:55 AM - edited 06-27-2025 07:55 AM
*** Script: Total calls: 1
*** Script: Total sum: 828
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2025 09:12 AM
try this
var supplierId = '792d9fc82b956690f90cf760ce91bf88';
// Use GlideAggregate for sum and count
var ga = new GlideAggregate('u_payfile');
ga.addQuery('u_supplier', supplierId);
ga.addNotNullQuery('u_new_call_amount');
ga.addAggregate('COUNT'); // Count all records
ga.addAggregate('SUM', 'u_new_call_amount'); // Sum of amounts
ga.query();
var totalCalls = 0;
var totalSum = 0;
if (ga.next()) {
totalCalls = parseInt(ga.getAggregate('COUNT')); // Correct: count of all records
totalSum = parseInt(ga.getAggregate('SUM', 'u_new_call_amount'));
}
gs.info('Total calls: ' + totalCalls);
gs.info('Total sum: ' + totalSum);
// Create/update summary record
var grSummary = new GlideRecord('u_payfile_summary');
grSummary.addQuery('u_partner_name', supplierId); // Ensure field matches reference
grSummary.query();
if (grSummary.next()) {
// Update existing record
grSummary.u_total_amount_payable = totalSum;
grSummary.u_total_no_of_calls = totalCalls;
grSummary.update();
} else {
// Insert new record
grSummary.initialize();
grSummary.u_partner_name = supplierId;
grSummary.u_total_amount_payable = totalSum;
grSummary.u_total_no_of_calls = totalCalls;
grSummary.insert();
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2025 06:51 AM
Hope you are doing good.
Did my reply answer your question?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2025 10:44 PM - edited 06-29-2025 11:19 PM
Hi @Ankur Bawiskar Thanks for all the responses so far but i am still getting the same response while using GlideAggregate, with below script i am getting expected result but i am doing
*** Script: Sum of calls are: 11267
*** Script: Total calls are: 155
*** Script: Total calls: 60
*** Script: Total sum: 0