The CreatorCon Call for Content is officially open! Get started here.

Incident form read-only when the state is 'In Progress', and ensure they become editable again when

Gajendra singh
Tera Contributor

I make all fields on the Incident form read-only when the state is 'In Progress', and ensure they become editable again when the state changes to something else?  I have tried this onChange script but it's working for when state is in progress. but revert to different then it not make it editable the field.

 

if (newValue == '2') {
        var fields = g_form.getEditableFields();
        for (var i = 0; i < fields.length; i++) {
            if (fields[i] !== 'state') {
                g_form.setReadOnly(fields[i], true);
            } else {
                g_form.setReadOnly(fields[i], false);
            }
        }
    } else {
var formElements = g_form.getFormElement().elements;
        for (var i = 0; i < formElements.length; i++) {
            var fieldName = formElements[i].name;
            g_form.setReadOnly(fieldName, false);
        }
}
6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@Gajendra singh 

try this once

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  if (isLoading) {
    return;
  }

  // State 'In Progress' (value = 2)
  if (newValue == '2') {
    var fields = g_form.getEditableFields();
    for (var i = 0; i < fields.length; i++) {
      if (fields[i] !== 'state') {
        g_form.setReadOnly(fields[i], true);
      } else {
        g_form.setReadOnly(fields[i], false);
      }
    }
  } else {
    // Only revert fields set by script; use getEditableFields not formElements
    var fields = g_form.getEditableFields();
    for (var i = 0; i < fields.length; i++) {
      g_form.setReadOnly(fields[i], false);
    }
  }
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

svirkar420
Tera Guru

Hi @Gajendra singh , Have you tried creating UI policies instead of the script, As this can be easily achieved using UI polices and it is the recommended practice by ServiceNow itself.

Create UI policy > save > create UI policy action for all field.

1. Add condition as - state is "in progress".

2. Make UI policy - check the box : Reverse if false.

This will work perfectly, if you want to achieve this using script only then try script provided by

Ankur.

 

If this answer helps you in any way make sure to Mark this as accepted solution and give a thumbs up this will also benefit others as well.
Regards.
Saurabh V.

Thanks @svirkar420 ,

 

I have tried With the UI Policy and it's working fine.

 

Thanks @Ankur Bawiskar 

 

I tried using the script with getEditableFields(), but it didn’t work as expected in the else part. The issue is that getEditableFields() only returns fields that are currently editable. Since only the 'state' field is editable at that point, the script doesn’t revert the other fields back to editable.

@Gajendra singh , I noticed you went with the UI Policy approach. Just curious - wouldn't it be simpler to handle this in the client script itself by either defining the fields in an array or just looping through all form elements? With UI Policies, don't you have to create separate policies for each field?

Is there a technical limitation I'm missing that makes the UI Policy better for this use case, or is it more of a preference thing?

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.