<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Onchange client script is getting triggered twice in Community Central forum</title>
    <link>https://www.servicenow.com/community/community-central-forum/onchange-client-script-is-getting-triggered-twice/m-p/3199317#M2404</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have an onChange Client script, when I change the value my logs show:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;test date selectedDate: 2025-03-06 18:17:45 - currentDate: 2025-03-07 18:17:48&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;[note: the format changed to 2025-06-03]&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;test date selectedDate: 2025-06-03 10:17:45 - currentDate: 2025-03-07 18:18:07&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;The form successfully saves and I see the below screen:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TuckWai_0-1741343169803.png" style="width: 219px;"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/424879iAC37C1ECA45DB2C9/image-dimensions/219x188?v=v2" width="219" height="188" role="button" title="TuckWai_0-1741343169803.png" alt="TuckWai_0-1741343169803.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I know the form was saved because I do a refresh and the newValue was saved as below:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TuckWai_1-1741343279033.png" style="width: 218px;"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/424880i1B454E520CD94A28/image-dimensions/218x44?v=v2" width="218" height="44" role="button" title="TuckWai_1-1741343279033.png" alt="TuckWai_1-1741343279033.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 07 Mar 2025 10:31:00 GMT</pubDate>
    <dc:creator>Tuck Wai</dc:creator>
    <dc:date>2025-03-07T10:31:00Z</dc:date>
    <item>
      <title>Onchange client script is getting triggered twice</title>
      <link>https://www.servicenow.com/community/community-central-forum/onchange-client-script-is-getting-triggered-twice/m-p/3199317#M2404</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have an onChange Client script, when I change the value my logs show:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;test date selectedDate: 2025-03-06 18:17:45 - currentDate: 2025-03-07 18:17:48&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;[note: the format changed to 2025-06-03]&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;test date selectedDate: 2025-06-03 10:17:45 - currentDate: 2025-03-07 18:18:07&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;The form successfully saves and I see the below screen:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TuckWai_0-1741343169803.png" style="width: 219px;"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/424879iAC37C1ECA45DB2C9/image-dimensions/219x188?v=v2" width="219" height="188" role="button" title="TuckWai_0-1741343169803.png" alt="TuckWai_0-1741343169803.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I know the form was saved because I do a refresh and the newValue was saved as below:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TuckWai_1-1741343279033.png" style="width: 218px;"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/424880i1B454E520CD94A28/image-dimensions/218x44?v=v2" width="218" height="44" role="button" title="TuckWai_1-1741343279033.png" alt="TuckWai_1-1741343279033.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Mar 2025 10:31:00 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/onchange-client-script-is-getting-triggered-twice/m-p/3199317#M2404</guid>
      <dc:creator>Tuck Wai</dc:creator>
      <dc:date>2025-03-07T10:31:00Z</dc:date>
    </item>
    <item>
      <title>Re: Onchange client script is getting triggered twice</title>
      <link>https://www.servicenow.com/community/community-central-forum/onchange-client-script-is-getting-triggered-twice/m-p/3199332#M2405</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/294889"&gt;@Tuck Wai&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It seems there is an onSubmit client script written on the same table. Please check that OnCubmit client script and make changes accordingly.&lt;/P&gt;&lt;P&gt;You got to list view of client script and put condition Script Type - OnSubmit , Table - Same Table in which Onchange Client script is written.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;If my response helped, please mark it helpful and accept the solution so that it benefits future readers.&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;BR /&gt;Rohit&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Mar 2025 10:47:58 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/onchange-client-script-is-getting-triggered-twice/m-p/3199332#M2405</guid>
      <dc:creator>Rohit  Singh</dc:creator>
      <dc:date>2025-03-07T10:47:58Z</dc:date>
    </item>
    <item>
      <title>Re: Onchange client script is getting triggered twice</title>
      <link>https://www.servicenow.com/community/community-central-forum/onchange-client-script-is-getting-triggered-twice/m-p/3199386#M2406</link>
      <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/294889"&gt;@Tuck Wai&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;please share the relevant scripts.&lt;/P&gt;
&lt;P&gt;what's your business requirement here?&lt;/P&gt;
&lt;P&gt;If my response helped please mark it correct and close the thread so that it benefits future readers.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Mar 2025 11:41:43 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/onchange-client-script-is-getting-triggered-twice/m-p/3199386#M2406</guid>
      <dc:creator>Ankur Bawiskar</dc:creator>
      <dc:date>2025-03-07T11:41:43Z</dc:date>
    </item>
    <item>
      <title>Re: Onchange client script is getting triggered twice</title>
      <link>https://www.servicenow.com/community/community-central-forum/onchange-client-script-is-getting-triggered-twice/m-p/3200119#M2414</link>
      <description>&lt;P&gt;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.&lt;BR /&gt;&lt;BR /&gt;client script:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;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;
        }
    }); 
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Script include:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;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 &amp;gt; 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());
    },&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Mar 2025 04:33:23 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/onchange-client-script-is-getting-triggered-twice/m-p/3200119#M2414</guid>
      <dc:creator>Tuck Wai</dc:creator>
      <dc:date>2025-03-08T04:33:23Z</dc:date>
    </item>
    <item>
      <title>Re: Onchange client script is getting triggered twice</title>
      <link>https://www.servicenow.com/community/community-central-forum/onchange-client-script-is-getting-triggered-twice/m-p/3200140#M2416</link>
      <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/294889"&gt;@Tuck Wai&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;did you identify that script then which is causing this onChange to run?&lt;/P&gt;
&lt;P&gt;If my response helped please mark it correct and close the thread so that it benefits future readers.&lt;/P&gt;</description>
      <pubDate>Sat, 08 Mar 2025 06:08:00 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/onchange-client-script-is-getting-triggered-twice/m-p/3200140#M2416</guid>
      <dc:creator>Ankur Bawiskar</dc:creator>
      <dc:date>2025-03-08T06:08:00Z</dc:date>
    </item>
  </channel>
</rss>

