How to separate processing before and after saving a record in a client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2024 11:11 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2024 12:19 AM
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
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-01-2024 04:23 PM
Hi @Runjay Patel
Thank you for your answer.
I tried this script, but the result was same as mine...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2024 12:40 AM
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:
Reduces Overhead:
- Instead of triggering logic on every change to the status field, the script runs only when the form is submitted.
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.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-01-2024 04:25 PM
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...