<?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 Re: Client script field msg both onchange and onload in Developer forum</title>
    <link>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695692#M1043422</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/275179"&gt;@CE_&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A client script cannot work onload and onchange. You will have to create 2 Client scripts.&lt;/P&gt;</description>
    <pubDate>Mon, 09 Oct 2023 08:08:55 GMT</pubDate>
    <dc:creator>Peter Bodelier</dc:creator>
    <dc:date>2023-10-09T08:08:55Z</dc:date>
    <item>
      <title>Client script both onchange and onload</title>
      <link>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695687#M1043421</link>
      <description>&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 08:14:31 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695687#M1043421</guid>
      <dc:creator>CE_</dc:creator>
      <dc:date>2023-10-31T08:14:31Z</dc:date>
    </item>
    <item>
      <title>Re: Client script field msg both onchange and onload</title>
      <link>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695692#M1043422</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/275179"&gt;@CE_&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A client script cannot work onload and onchange. You will have to create 2 Client scripts.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 08:08:55 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695692#M1043422</guid>
      <dc:creator>Peter Bodelier</dc:creator>
      <dc:date>2023-10-09T08:08:55Z</dc:date>
    </item>
    <item>
      <title>Re: Client script field msg both onchange and onload</title>
      <link>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695696#M1043423</link>
      <description>&lt;P&gt;Certainly! To achieve this behavior, you can use a client script in ServiceNow. Here's the client script code that you can use to handle the onChange and onLoad events for the "agreement" field:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;function onLoad() {

    checkAgreement();

}



function onChange(control, oldValue, newValue, isLoading) {

    if (!isLoading) {

        checkAgreement();

    }

}



function checkAgreement() {

    var agreementField = g_form.getControl('agreement');

    var agreementValue = agreementField.value;

    

    var message1 = g_form.getMessages('message1');

    var message2 = g_form.getMessages('message2');



    if (agreementValue === 'none') {

        if (message1.length === 0) {

            g_form.addInfoMessage('message1');

        }

        if (message2.length &amp;gt; 0) {

            g_form.removeMessage('message2');

        }

    } else if (agreementValue === 'yes') {

        if (message2.length === 0) {

            g_form.addInfoMessage('message2');

        }

        if (message1.length &amp;gt; 0) {

            g_form.removeMessage('message1');

        }

    } else if (agreementValue === 'no') {

        // If 'no' is chosen, no message is shown

        if (message1.length &amp;gt; 0) {

            g_form.removeMessage('message1');

        }

        if (message2.length &amp;gt; 0) {

            g_form.removeMessage('message2');

        }

    }

}
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this script:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- The `onLoad` function is triggered when the form is loaded.&lt;/P&gt;&lt;P&gt;- The `onChange` function is triggered whenever the "agreement" field value changes.&lt;/P&gt;&lt;P&gt;- The `checkAgreement` function checks the value of the "agreement" field and shows/removes messages accordingly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Make sure to replace `'agreement'` with the actual name of your field in ServiceNow. Also, replace `'message1'` and `'message2'` with the actual names of your sys_ui_message records.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This script should cover the behavior you described: showing specific messages based on the selected value of the "agreement" field. Remember to test the script thoroughly in your ServiceNow instance to ensure it behaves as expected.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Mark my answer helpful &amp;amp; accepted if it helps you resolve your query.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Danish&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 08:12:31 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695696#M1043423</guid>
      <dc:creator>Danish Bhairag2</dc:creator>
      <dc:date>2023-10-09T08:12:31Z</dc:date>
    </item>
    <item>
      <title>Re: Client script field msg both onchange and onload</title>
      <link>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695737#M1043434</link>
      <description>&lt;P&gt;Thanks, I'll try this. Why do I need these:&amp;nbsp; (message1.length === 0) and these if (message2.length &amp;gt; 0)?&lt;BR /&gt;Can you explain what these does, why I cant write like this:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;    if (agreementValue === 'none') {


            g_form.addInfoMessage('message1');&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 08:34:58 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695737#M1043434</guid>
      <dc:creator>CE_</dc:creator>
      <dc:date>2023-10-09T08:34:58Z</dc:date>
    </item>
    <item>
      <title>Re: Client script field msg both onchange and onload</title>
      <link>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695741#M1043436</link>
      <description>&lt;P&gt;How would these 2 client script look like?&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 08:38:17 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695741#M1043436</guid>
      <dc:creator>CE_</dc:creator>
      <dc:date>2023-10-09T08:38:17Z</dc:date>
    </item>
    <item>
      <title>Re: Client script field msg both onchange and onload</title>
      <link>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695753#M1043438</link>
      <description>&lt;P&gt;You could use the script provided by&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/412195"&gt;@Danish Bhairag2&lt;/a&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just split them between 2 client scripts.&lt;BR /&gt;It seems it can work. I wouldn't create it like this though, because you won't be able to see that is also works onload, unless you look at the script. That is not very maintainable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since you will be reusing the bulk part of the script, it may be a good idea, to use a script include for that part, even though it is not needed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;function onLoad() {

   function checkAgreement() {

    var agreementField = g_form.getControl('agreement');

    var agreementValue = agreementField.value;

    

    var message1 = g_form.getMessages('message1');

    var message2 = g_form.getMessages('message2');



    if (agreementValue === 'none') {

        if (message1.length === 0) {

            g_form.addInfoMessage('message1');

        }

        if (message2.length &amp;gt; 0) {

            g_form.removeMessage('message2');

        }

    } else if (agreementValue === 'yes') {

        if (message2.length === 0) {

            g_form.addInfoMessage('message2');

        }

        if (message1.length &amp;gt; 0) {

            g_form.removeMessage('message1');

        }

    } else if (agreementValue === 'no') {

        // If 'no' is chosen, no message is shown

        if (message1.length &amp;gt; 0) {

            g_form.removeMessage('message1');

        }

        if (message2.length &amp;gt; 0) {

            g_form.removeMessage('message2');

        }

    }

}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;function onChange(control, oldValue, newValue, isLoading) {

   function checkAgreement() {

    var agreementField = g_form.getControl('agreement');

    var agreementValue = agreementField.value;

    

    var message1 = g_form.getMessages('message1');

    var message2 = g_form.getMessages('message2');



    if (agreementValue === 'none') {

        if (message1.length === 0) {

            g_form.addInfoMessage('message1');

        }

        if (message2.length &amp;gt; 0) {

            g_form.removeMessage('message2');

        }

    } else if (agreementValue === 'yes') {

        if (message2.length === 0) {

            g_form.addInfoMessage('message2');

        }

        if (message1.length &amp;gt; 0) {

            g_form.removeMessage('message1');

        }

    } else if (agreementValue === 'no') {

        // If 'no' is chosen, no message is shown

        if (message1.length &amp;gt; 0) {

            g_form.removeMessage('message1');

        }

        if (message2.length &amp;gt; 0) {

            g_form.removeMessage('message2');

        }

    }

}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 08:44:12 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695753#M1043438</guid>
      <dc:creator>Peter Bodelier</dc:creator>
      <dc:date>2023-10-09T08:44:12Z</dc:date>
    </item>
    <item>
      <title>Re: Client script field msg both onchange and onload</title>
      <link>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695764#M1043443</link>
      <description>&lt;P&gt;Thanks, But does the onLoad script have to be that long?&amp;nbsp;&lt;BR /&gt;None is the onLoad value so then I wouldnt need any if statements I guess?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 08:48:33 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695764#M1043443</guid>
      <dc:creator>CE_</dc:creator>
      <dc:date>2023-10-09T08:48:33Z</dc:date>
    </item>
    <item>
      <title>Re: Client script field msg both onchange and onload</title>
      <link>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695769#M1043447</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/275179"&gt;@CE_&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can modify the script logic as per your requirement &amp;amp; check if it works or not&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Danish&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 08:52:01 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695769#M1043447</guid>
      <dc:creator>Danish Bhairag2</dc:creator>
      <dc:date>2023-10-09T08:52:01Z</dc:date>
    </item>
    <item>
      <title>Re: Client script field msg both onchange and onload</title>
      <link>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695793#M1043456</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/275179"&gt;@CE_&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;You can use onChange client script for both onChange and onLoading&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading) {
      // your onLoad client script
   }
   // your onChange client script   
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;Please mark my answer correct &amp;amp; helpful, if it helps you&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Thank you&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 09:20:25 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695793#M1043456</guid>
      <dc:creator>RAMANA MURTHY G</dc:creator>
      <dc:date>2023-10-09T09:20:25Z</dc:date>
    </item>
    <item>
      <title>Re: Client script field msg both onchange and onload</title>
      <link>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695867#M1043480</link>
      <description>&lt;P&gt;You can remove those&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/275179"&gt;@CE_&lt;/a&gt;&amp;nbsp;. The way u have suggested that is also correct.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Danish&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 10:07:34 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/client-script-both-onchange-and-onload/m-p/2695867#M1043480</guid>
      <dc:creator>Danish Bhairag2</dc:creator>
      <dc:date>2023-10-09T10:07:34Z</dc:date>
    </item>
  </channel>
</rss>

