Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Client script both onchange and onload

CE_
Tera Contributor



9 REPLIES 9

Not applicable

Hi @CE_,

 

A client script cannot work onload and onchange. You will have to create 2 Client scripts.

How would these 2 client script look like?

Not applicable

You could use the script provided by @Danish Bhairag2.

 

Just split them between 2 client scripts.
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.

 

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.

 

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 > 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');

        }

    }

}

 

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 > 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');

        }

    }

}

 

 

 

Thanks, But does the onLoad script have to be that long? 
None is the onLoad value so then I wouldnt need any if statements I guess?