Add time logged on record to the custom field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 09:58 AM
Hello All,
I have created a custom field called 'Actual Effort' on custom table record. So, now whatever the time logged to that record should be added to the 'Actual Effort' field.
Users log their time under 'Time Cards' related list. so, any time logged on that record should be added and displayed on the 'Actual Effort' field.
How can I achieve this? how should the code look like?
Thanks,
Rocky.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 10:06 AM
Hi @Rocky5
I think you can use a Business Rule in ServiceNow to automatically calculate and update the 'Actual Effort' field whenever a new time entry is logged in the 'Time Cards' related list.
Script can be something like this -
(function executeRule(current, previous /*null when async*/) {
// Create a GlideAggregate to sum 'Time Worked' from related 'Time Cards'
var aggregate = new GlideAggregate('time_card');
aggregate.addAggregate('SUM', 'time_worked');
aggregate.addQuery('custom_table_field', current.sys_id); // Update 'custom_table_field' to match your reference field name
aggregate.query();
// Get the sum of 'Time Worked'
var actualEffort = 0;
if (aggregate.next()) {
actualEffort = aggregate.getAggregate('SUM', 'time_worked');
}
// Update the 'Actual Effort' field on the custom table record
current.u_actual_effort = actualEffort;
})(current, previous);
Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!
Regards,
Tushar