Onchange client script is getting triggered twice
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2025 02:31 AM
Hi,
I have an onChange Client script, when I change the value my logs show:
test date selectedDate: 2025-03-06 18:17:45 - currentDate: 2025-03-07 18:17:48 |
This is correct because the format is the same and I can do a comparison, which validates the selected date and no error pops out. Therefore I am able to proceed and save. However when I save, I get this log:
[note: the format changed to 2025-06-03]
test date selectedDate: 2025-06-03 10:17:45 - currentDate: 2025-03-07 18:18:07 |
The form successfully saves and I see the below screen:
I know the form was saved because I do a refresh and the newValue was saved as below:
I also know that the error pops after saving because the system is comparing june 3rd instead of march 6. How on earth is this happening and how do I solve it?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2025 02:47 AM
Hi @Tuck Wai ,
It seems there is an onSubmit client script written on the same table. Please check that OnCubmit client script and make changes accordingly.
You got to list view of client script and put condition Script Type - OnSubmit , Table - Same Table in which Onchange Client script is written.
If my response helped, please mark it helpful and accept the solution so that it benefits future readers.
Regards,
Rohit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2025 03:41 AM
please share the relevant scripts.
what's your business requirement here?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2025 08:31 PM - edited 03-07-2025 08:33 PM
I have already achieved my objective to prevent future date, its just I think something is changing my closed date field after I save and that triggers my client script the second time. To add on, this does not happen in classic UI, just in workspace.
client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('...');
ga.addParam('sysparm_name', 'isFutureDate');
ga.addParam('sysparm_selectedDate', newValue);
ga.getXMLAnswer(function(answer) {
if (answer == 'true') {
g_form.clearValue('closed_at');
g_form.addErrorMessage('Future date and time not allowed');
return;
}
});
}
Script include:
isFutureDate: function() {
var sDate = this.getParameter('sysparm_selectedDate');
var result = false;
var selectedDate = new GlideDateTime(this.formatDate(sDate));
var currentDate = new GlideDateTime();
currentDate.addSeconds(28800);
gs.info('test date selectedDate: ' + selectedDate + ' - currentDate: ' + currentDate);
if (selectedDate > currentDate) {
result = true;
}
return result;
},
formatDate: function(sDate) {
var parts = sDate.split(" ");
var datePart = parts[0].split("-");
var timePart = parts[1];
// Rearrange date components to 'yyyy-dd-MM'
var formattedDate = datePart[2] + "-" + datePart[1] + "-" + datePart[0] + " " + timePart;
return formattedDate;
//var selectedDate = new GlideDateTime(formattedDate.toString());
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2025 10:08 PM
did you identify that script then which is causing this onChange to run?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader