Set all other fields read only when state is cancelled

moni170
Tera Contributor

I'm trying to set all fields read only when my state on incident form changes to canceled!!

Here's the code i've written and i'm currently using utah version but this code isn't working. Is there any possibility that getEditablefields() function doesn't work anymore?

If there's any other solution, do let me acknowledge.

That'll will be a great help.

1 ACCEPTED SOLUTION

DYCM
Mega Sage

Everything you have done is correct except for loop, you didn't set an original value to i.  1.png

Please replace: for (var i;i<field.length;i++) with for (var i=0;i<field.length;i++)

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   if(newValue == '8'){
	var field = g_form.getEditableFields();
	for (var i=0;i<field.length;i++){
		g_form.setReadOnly(field[i],true);
	}
   }
   
}

 

 

 

View solution in original post

6 REPLIES 6

Shruti
Mega Sage
Mega Sage

Hi,

Can you try to create a UI policy and put the code in Scripts -> run script -> execute if true script

UI policy condition -> State is cancelled

Anubhav24
Mega Sage
Mega Sage

Hi @moni170 ,

 

You can use table.* ACL to make all fields read only when state is cancelled.

Please refer to below URL as well :

https://www.servicenowelite.com/blog/2019/10/2/access-controls

Alka_Chaudhary
Mega Sage
Mega Sage

Hello @moni170 ,

You can create a UI Policy :-

Alka4_0-1697450898014.png

And in the script section you can write the below script:-

Alka4_1-1697450971390.png

function onCondition() {
    var fields = g_form.getEditableFields();
    for (var x = 0; x < fields.length; x++) {
        var field = g_form.getValue(fields[x]);
        g_form.setReadOnly(fields[x], true);
    }
}

Please mark this comment as Correct Answer/Helpful if it helped you.
Regards,
Alka

DYCM
Mega Sage

Everything you have done is correct except for loop, you didn't set an original value to i.  1.png

Please replace: for (var i;i<field.length;i++) with for (var i=0;i<field.length;i++)

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   if(newValue == '8'){
	var field = g_form.getEditableFields();
	for (var i=0;i<field.length;i++){
		g_form.setReadOnly(field[i],true);
	}
   }
   
}