I wanted to make all editable fields readonly, after incident state is closed.

aravind1patil1
Tera Contributor

Dear Community Members

 

I wanted to make all editable fields readonly, after incident state is closed.

 

Can someone provide me clientscript for this requirement. 

 

Please help, thanks for your support

2 ACCEPTED SOLUTIONS

Sandesh Powar
Kilo Sage

Hi @aravind1patil1 ,

You can achieve this using an onLoad Client Script on the Incident table.

function onLoad() {
    // Replace '7' with the actual choice value for your 'Closed' state if it differs
    var state = g_form.getValue('state');
    
    if (state == '7') { 
        var fields = g_form.getEditableFields();
        for (var i = 0; i < fields.length; i++) {
            // Keep the state field itself editable or make everything read-only based on your preference
            g_form.setReadOnly(fields[i], true);
        }
    }
}

 

Hope this helps! and resolves your issue, please mark the response as helpful and accept it as the solution.

Regards,
Sandesh Powar
ServiceNow Rising Star

 

View solution in original post

Juhi Poddar
Kilo Patron

Hello @aravind1patil1 

 

Out of the box, closed incidents are usually already read only for itil users. This comes from a UI policy named Make fields read-only on close (table: sys_ui_policy) that checks the state.

Please check whether it is inactive or modified on your instance, because that may be the real reason the form is editable.

 

If you still need a client script, create an onLoad client script on the Incident table with the code below.

function onLoad() { 
// 7 is the default value of Closed, please check it on your instance 
   if (g_form.getValue('state') == '7') { 
       var fields = g_form.getEditableFields(); 
       for (var i = 0; i < fields.length; i++) { 
           g_form.setReadOnly(fields[i], true); 
       } 
   } 
}

Hope this helps!

 

Thank You!

Juhi Poddar

 

View solution in original post

6 REPLIES 6

Syed14
Giga Guru

Hi, 
Always try to figure things out on your own first, as it’s a great way to build your skills. If you get stuck at any point, you can ask for help anytime. 🙂

Here is the code:

function onLoad() {

    // Check if Incident state is Closed
    if (g_form.getValue('state') == '7') {

        // Get all fields on the form
        var fields = g_form.getEditableFields();

        // Make all editable fields read-only
        for (var i = 0; i < fields.length; i++) {
            g_form.setReadOnly(fields[i], true);
        }
    }
}

 

Aditya_hublikar
Giga Sage

Hello @aravind1patil1 ,

 

You can use onload client script :

Aditya_hublikar_0-1793940251089.png

 

 

 

function onLoad() {
   //Type appropriate comment here, and begin script below
   if(g_form.getValue('state')=='7')
   {
	var data=g_form.getEditableFields();
	for( var i=0; i < data.length;i++)
	{
		g_form.setReadOnly(data[i],true)
	}

   }
  
    
}

Aditya_hublikar_1-1793940268963.png

 

 

If this helps you then mark it as helpful and accept as solution .

Regards,

Aditya

Sandesh Powar
Kilo Sage

Hi @aravind1patil1 ,

You can achieve this using an onLoad Client Script on the Incident table.

function onLoad() {
    // Replace '7' with the actual choice value for your 'Closed' state if it differs
    var state = g_form.getValue('state');
    
    if (state == '7') { 
        var fields = g_form.getEditableFields();
        for (var i = 0; i < fields.length; i++) {
            // Keep the state field itself editable or make everything read-only based on your preference
            g_form.setReadOnly(fields[i], true);
        }
    }
}

 

Hope this helps! and resolves your issue, please mark the response as helpful and accept it as the solution.

Regards,
Sandesh Powar
ServiceNow Rising Star

 

Juhi Poddar
Kilo Patron

Hello @aravind1patil1 

 

Out of the box, closed incidents are usually already read only for itil users. This comes from a UI policy named Make fields read-only on close (table: sys_ui_policy) that checks the state.

Please check whether it is inactive or modified on your instance, because that may be the real reason the form is editable.

 

If you still need a client script, create an onLoad client script on the Incident table with the code below.

function onLoad() { 
// 7 is the default value of Closed, please check it on your instance 
   if (g_form.getValue('state') == '7') { 
       var fields = g_form.getEditableFields(); 
       for (var i = 0; i < fields.length; i++) { 
           g_form.setReadOnly(fields[i], true); 
       } 
   } 
}

Hope this helps!

 

Thank You!

Juhi Poddar