Client script both onchange and onload

CE_
Tera Contributor



9 REPLIES 9

Hi @CE_ ,

 

You can modify the script logic as per your requirement & check if it works or not

 

Thanks,

Danish

Danish Bhairag2
Tera Sage
Tera Sage

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:

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 > 0) {

            g_form.removeMessage('message2');

        }

    } else if (agreementValue === 'yes') {

        if (message2.length === 0) {

            g_form.addInfoMessage('message2');

        }

        if (message1.length > 0) {

            g_form.removeMessage('message1');

        }

    } else if (agreementValue === 'no') {

        // If 'no' is chosen, no message is shown

        if (message1.length > 0) {

            g_form.removeMessage('message1');

        }

        if (message2.length > 0) {

            g_form.removeMessage('message2');

        }

    }

}

 

In this script:

 

- The `onLoad` function is triggered when the form is loaded.

- The `onChange` function is triggered whenever the "agreement" field value changes.

- The `checkAgreement` function checks the value of the "agreement" field and shows/removes messages accordingly.

 

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.

 

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.

 

Mark my answer helpful & accepted if it helps you resolve your query.

 

Thanks,

Danish

Thanks, I'll try this. Why do I need these:  (message1.length === 0) and these if (message2.length > 0)?
Can you explain what these does, why I cant write like this:

    if (agreementValue === 'none') {


            g_form.addInfoMessage('message1');

  

You can remove those @CE_ . The way u have suggested that is also correct.

 

Thanks,

Danish

RAMANA MURTHY G
Mega Sage
Mega Sage

Hello @CE_ ,

You can use onChange client script for both onChange and onLoading

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading) {
      // your onLoad client script
   }
   // your onChange client script   
}

 

Please mark my answer correct & helpful, if it helps you

Thank you

 

 

Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer