Block Save/Update when Activity Stream Post button is greyed out — Time Logging Enforcement

Luna123
Kilo Contributor

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

2 REPLIES 2

Tanushree Maiti
Tera Patron

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

  1. Navigate to System Definition > Client Scripts> New.
  2. Name: Enforce Time Logging on Notes
  3. Table: specific tables like Incident or Change Request //update table
  4. 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

    }

}

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

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.