not able to make variables Readonly

karan15
Tera Contributor

i am using attached conditions and script but its not making variables read only on RITM 

 

what could be the case ?

 

Please help 

1 ACCEPTED SOLUTION

Devender Kumar
Tera Guru
Tera Guru

Hi @karan15 

Please use something like this

function onLoad() {
   g_form.setVariablesReadOnly(true);   
}

 

UI Type: ALL

 

 

If above information resolved your issue, please don't forget to mark correct or helpful.

Regards

Devender

View solution in original post

4 REPLIES 4

Devender Kumar
Tera Guru
Tera Guru

Hi @karan15 

Please use something like this

function onLoad() {
   g_form.setVariablesReadOnly(true);   
}

 

UI Type: ALL

 

 

If above information resolved your issue, please don't forget to mark correct or helpful.

Regards

Devender

thanks Devender this worked 

DUGGI
Giga Guru

@karan15 

Copied from the KB0752560

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0752560

 

 
 

Description

Trying to make a Variable Editor on the incident table read-only does not work with the setVariablesReadOnly method.

Steps to reproduce:

  1. Create an onLoad client script on the Incident table with g_form.setVariablesReadOnly as true
  2. Open the Incident form that has the variable editor
  3. Notice the variables in Variable editor on Incident are still editable

Release or Environment

Kingston, London, Madrid

 

Cause

The setVariablesReadOnly method is specifically reserved for the sc_task and the sc_req_item table.  Will not work with any other table

Resolution

To set the Variable Editor to read only a different table like incident, please follow these steps:

  1. Create a Client Script on the Incident table
  2. Paste the following script:
    function onLoad() { 
    $("variable_map").querySelectorAll("item").forEach(function(item){
    var variable = item.getAttribute("qname"); 
    g_form.setReadOnly("variables."+ variable, true); 
    }); 
    }
  3. This will require to uncheck (set to false) the "isolate script" checkbox (This checkbox is not on the form by default) Steps below how to bring it to the form)
    1. To do that right click in the header -> Configure -> Form Layout
    2. On the left side find "Isolate script" and bring it to the right and save
  4. Save changes
    This should make the Variable Editor on the incident table read-only

VedanshRana
Tera Contributor

Hi @karan15 ,

 

Ideally Variables don't have the option to make read only.

 

You have to create an onload client , 

Also check the: 

 Applies on Requested Items
 
Now in the script section, add the below piece of code:
function onLoad() {
         g_form.setReadOnly('YOUR VARIABLE NAME', true);
}
 
Note: If you have made the variable as mandatory from the variable level then you would not be able to make it read only. Before making read only you have to make the variable as non mandatory then make it read only, using below code:
 
function onLoad() {
         g_form.setMandatory('YOUR VARIABLE NAME', false);
         g_form.setReadOnly('YOUR VARIABLE NAME', true);
}
 
If you were able to resolve your issue, please don't forget to mark correct or helpful.