How to separate processing before and after saving a record in a client script

yuitanakau6
Kilo Contributor

I want to automatically input the current date and time into the "Response date and time" field if the status is changed from another value to "Answered" (value: 4), and "Response date and time" field is empty.

Additionally, if the status is changed to something other than "Answered" while editing the record (before saving), I want the "Response date and time" field to be cleared. This is to handle cases where the status was mistakenly set to "Answered" instead of "On Hold," for example.

Currently, when a record is saved with the "Response date and time" field filled in, if the status is changed to "Answered" or another status after saving, the "Response date and time" field id cleared even though it already has a value (is not empty)

My script is below.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
 
    var u_glide_date_time_2 = g_form.getValue('u_glide_date_time_2');
 
    if (newValue == '4' && g_form.getValue('u_glide_date_time_2') === '') {
        if (u_glide_date_time_2 == '') {
            var currentDate = new Date();
            var formattedDateTime = formatDateTime(currentDate);
 
            g_form.setValue('u_glide_date_time_2', formattedDateTime);
        } else {
            // g_form.clearValue('u_glide_date_time_2');
        }
    } else {
        g_form.clearValue('u_glide_date_time_2');
    }
}
 
function formatDateTime(date) {
    var year = date.getFullYear();
    var month = ('0' + (date.getMonth() + 1)).slice(-2);
    var day = ('0' + date.getDate()).slice(-2);
    var hours = ('0' + date.getHours()).slice(-2);
    var minutes = ('0' + date.getMinutes()).slice(-2);
    var seconds = ('0' + date.getSeconds()).slice(-2);
    return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
}
4 REPLIES 4

Runjay Patel
Giga Sage

Hi @yuitanakau6 ,

 

Try using below code.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    // Reference the "Response date and time" field
    var responseDateTimeField = 'u_glide_date_time_2';
    var responseDateTimeValue = g_form.getValue(responseDateTimeField);

    if (newValue === '4') {
        // If status is changed to "Answered" and the "Response date and time" field is empty
        if (!responseDateTimeValue) {
            var currentDate = new Date();
            g_form.setValue(responseDateTimeField, formatDateTime(currentDate));
        }
    } else if (oldValue === '4') {
        // If the status is changed away from "Answered" (and previously was "Answered")
        g_form.clearValue(responseDateTimeField);
    }
}

// Utility function to format date and time as "YYYY-MM-DD HH:mm:ss"
function formatDateTime(date) {
    var year = date.getFullYear();
    var month = ('0' + (date.getMonth() + 1)).slice(-2);
    var day = ('0' + date.getDate()).slice(-2);
    var hours = ('0' + date.getHours()).slice(-2);
    var minutes = ('0' + date.getMinutes()).slice(-2);
    var seconds = ('0' + date.getSeconds()).slice(-2);
    return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
}

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Hi @Runjay Patel 
Thank you for your answer.
I tried this script, but the result was same as mine...

Juhi Poddar
Kilo Patron

Hello @yuitanakau6 

You can try onsubmit client script:

function onSubmit() {
    var statusField = 'state'; // Replace with your status field's name
    var responseDateTimeField = 'u_glide_date_time_2';
    var statusValue = g_form.getValue(statusField);
    var responseDateTimeValue = g_form.getValue(responseDateTimeField);

    if (statusValue == '4') { // Status is "Answered"
        if (!responseDateTimeValue) {
            // Populate the field only if it is empty
            var currentDate = new Date();
            g_form.setValue(responseDateTimeField, formatDateTime(currentDate));
        }
    } else {
        // Clear the field if status is not "Answered"
        g_form.clearValue(responseDateTimeField);
    }
    return true; // Allow form submission
}

// Helper function to format the date and time in the desired format
function formatDateTime(date) {
    var year = date.getFullYear();
    var month = ('0' + (date.getMonth() + 1)).slice(-2);
    var day = ('0' + date.getDate()).slice(-2);
    var hours = ('0' + date.getHours()).slice(-2);
    var minutes = ('0' + date.getMinutes()).slice(-2);
    var seconds = ('0' + date.getSeconds()).slice(-2);
    return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
}

How onSubmit Improves the Logic:

  1. Reduces Overhead:

    • Instead of triggering logic on every change to the status field, the script runs only when the form is submitted.
  2. Handles Intermediate Changes:

    • Any changes made during editing (e.g., mistakenly setting the status to "Answered" and correcting it) won't unnecessarily populate or clear the field before submission.
  3. Accurate Data at Submission:

    • The field value is set or cleared based on the final state of the status field.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

Hi @Juhi Poddar 
Thank you for your answer.
I tried this script but this didn't work.
 "Response date and time" field was not filled automatically...