Block Save/Update when Activity Stream Post button is greyed out — Time Logging Enforcement
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi Community,
We have a requirement to enforce time logging on Task-based forms (Incident, Change, etc.) in ServiceNow.
What we have built:
A UI Script that adds Hrs/Mins input fields next to the Activity Stream Post button
Post button is greyed out until Hrs/Mins are entered
When Post is clicked, time is logged via GlideAjax to task_time_worked
This works perfectly
Requirement:
When the Post button is greyed out (meaning user has typed a comment/work note but has NOT logged time), we want to block the Save/Update button on the form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Luna123
To enforce time logging when adding comments/work notes, you can use an onSubmit Client Script combined with a Script Include.
1: Create a Script Include
- Navigate to System Definition > Script Includes> New.
- Name: TaskTimeValidator
- Client callable: Check the box.
Sample code:
var TaskTimeChecker= Class.create();
TaskTimeChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {
hasLoggedTime: function() {
var taskId = this.getParameter('sysparm_task_id');
var grTime = new GlideRecord('task_time_worked');
grTime.addQuery('task', taskId);
grTime.query();
return grTime.hasNext();
},
type: 'TaskTimeValidator'
});
2: Create an onSubmit Client Script
- Navigate to System Definition > Client Scripts> New.
- Name: Enforce Time Logging on Notes
- Table: specific tables like Incident or Change Request //update table
- Type: onSubmit
Sample script:
function onSubmit() {
var comments = g_form.getValue('comments');
var workNotes = g_form.getValue('work_notes');
if (g_form.isNewRecord() || (comments == '' && workNotes == '')) {
return true;
}
var ga = new GlideAjax('TaskTimeChecker');
ga.addParam('sysparm_name', 'hasLoggedTime');
ga.addParam('sysparm_task_id', g_form.getUniqueValue());
ga.getXMLWait();
var timeLogged = ga.getAnswer();
if (timeLogged !== 'true') {
g_form.addErrorMessage("You must log time (Time Worked) before saving when adding comments or work notes.");
return false; // Blocks the Save/Update
}
}
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Thanks for the response! However, this solution checks if any time entry exists for the task — which means if a user logged time last week, they can still save today without logging time again.
Our specific requirement is:
- We have Hrs/Mins fields added next to the Post button via UI Script
- Post button is greyed out until Hrs/Mins are entered
- We want to block Save ONLY when Post button is currently greyed out — meaning user typed a comment but did NOT enter Hrs/Mins that time he can't save also form.
The challenge is passing this session-level state from UI Script to onSubmit Client Script reliably.