Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Onchange client script to set value on hidden variable

CE_
Tera Contributor

Hi,

I have a hidden field in a form that I need to set value on. Is this possible with on change script?
If variable A has answer "no" then variable B is not visible. But variable B should then be set to "abc". 
So can I use on change client script to set value on this hidden variable? I tried but got "Javascript error in your browser console".

Thanks,

9 REPLIES 9

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

You can try to make the field visible, set the value and then hide it again.

-Anurag

Prashant Ahire
Kilo Sage

Hello @CE_,

Yes, this is possible. 

Just you have to set variable B to "abc" before making the field hidden.

I tired this in my instance:- 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var varOne = g_form.getValue('variable_1');
    if (varOne == 'No') {
        g_form.setValue('variable_2', 'Test');   //First set the value you want
        g_form.setDisplay('variable_2', false);  // then hide the field

    } else {
        g_form.setDisplay('variable_2', true);
    }
    //Type appropriate comment here, and begin script below

}
 
Please check and Mark Correct and Helpful if it really helps you.
Regards,
Prashant Ahire

 

 

Danish Bhairag2
Tera Sage

Hi @CE_ ,

 

Hope you are using UI policy to do this? If variable A has answer "no" then variable B is not visible

 

if Yes then in the UI policy itself u can mention under script tab if condition is true then

g_form.setValue('B','abc');

 

Thanks,

Danish

 

Tai Vu
Kilo Patron
Kilo Patron

Hi @CE_ 

If you've hidden your variable by enabling the Hidden checkbox, you can still set its value using the following code:

g_form.setValue('<variable_name>', '<value>');

 

The error in your browser console might be due to other causes. Can you share what you have done so far?

 

Cheers,

Tai Vu