multiple choice values read only

Basina Akash
Tera Contributor

I want to set one of the choice variable value to read only based on the another variable in client script

10 REPLIES 10

Kartik Magadum
Kilo Sage

Hello @Basina Akash 

To establish a variable as read-only depending on another variable, you can employ a Catalog UI policy or a Catalog client script. In the OnChange client script, you can use the following code:

g_form.setReadOnly('variable_name', true);

If you wish to conceal a choice based on a variable, you can use the following line of code:

g_form.removeOption('fieldName', 'choiceValue');

 

Please Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

 

Thanks & Regards,

Kartik Magadum

SLK Gupta
Tera Guru

Hi @Basina Akash ,

 

as you want to make a field read only, when another field changes, the best way is to use an UI Policy and UI Policy Action, But as you said you want to make it using client script you can use on change client script and use below function 

 

// below script is from product documentation @Basina Akash 

 

 

// Create a Client Script on a table (e.g., incident) and paste this script
// Uncheck (set to false) the "isolate script" checkbox (not available by default)
// To add the isolate script checkbox to the form, configure form layout to add the checkbox
function onLoad() {
$("variable_map").querySelectorAll("item").forEach(function(item){
var variable = item.getAttribute("qname");
g_form.setReadOnly("variables."+ variable, true);
});
}

 

SLKGupta_0-1704109429375.png

You can refer below link as well:
https://docs.servicenow.com/bundle/vancouver-api-reference/page/app-store/dev_portal/API_reference/G....

 

if my response helps you in fulfilling your requirement, please mark my answer as accepted solution or helpful @Basina Akash .

 

You can also refer this link for client script for further understanding @Basina Akash 

https://www.servicenow.com/community/developer-forum/how-to-make-setreadonly-function-work-for-clien...

 

Thanks & Regards,

S. Lakshmikanth Gupta.

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Basina Akash ,

 

You can create a onChange script n use the below code in it

 

if (newValue == "some_value") {// use appropriate values here

        // Set the choice variable to read-only

        g_form.setReadonly(readOnlyVariable, true);// use proper backend name of the fields

    } else {

        // Set the choice variable to editable

        g_form.setReadonly(readOnlyVariable, false);

    }

 

Thanks,

Danish

 

Basina Akash
Tera Contributor

I need only one of the choice field value not the entire variable , Only one of the value