Onchange client script is getting triggered twice

Tuck Wai
Tera Contributor

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:

TuckWai_0-1741343169803.png

I know the form was saved because I do a refresh and the newValue was saved as below:

TuckWai_1-1741343279033.png

 

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?

4 REPLIES 4

Rohit  Singh
Mega Sage

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

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Tuck Wai 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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());
    },

 

 

@Tuck Wai 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader