Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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);
        }
}
1 ACCEPTED SOLUTION

M Iftikhar
Tera Sage

@Gajendra singh Try below onchange script on incident table for state field.

getEditableFields() only returns fields that are currently editable. When fields are already read-only, they won't be in the list, so your loop can't make them editable again.

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

function toggleFields() {
    var state = g_form.getValue('state');
    var makeReadOnly = (state == '2');
    // Get ALL fields on the form, not just editable ones
    var allFields = g_form.elements;
    for (var i = 0; i < allFields.length; i++) {
        var fieldName = allFields[i].fieldName; 
        // Skip the state field itself
        if (fieldName != 'state') {
            g_form.setReadOnly(fieldName, makeReadOnly);
        }
    }
    // Ensure state field is always editable
    g_form.setReadOnly('state', false);
}

 

MIftikhar_0-1761210352362.png

 

MIftikhar_1-1761210359294.png

 

Thanks & Regards,
Muhammad Iftikhar

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

View solution in original post

6 REPLIES 6

Hi @Gajendra singh , Glad my answer helped you, Can you please mark my answer as an Accepted Solution. As it might prove helpful for others as well.

 

Regards,

Saurabh V.

 

M Iftikhar
Tera Sage

@Gajendra singh Try below onchange script on incident table for state field.

getEditableFields() only returns fields that are currently editable. When fields are already read-only, they won't be in the list, so your loop can't make them editable again.

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

function toggleFields() {
    var state = g_form.getValue('state');
    var makeReadOnly = (state == '2');
    // Get ALL fields on the form, not just editable ones
    var allFields = g_form.elements;
    for (var i = 0; i < allFields.length; i++) {
        var fieldName = allFields[i].fieldName; 
        // Skip the state field itself
        if (fieldName != 'state') {
            g_form.setReadOnly(fieldName, makeReadOnly);
        }
    }
    // Ensure state field is always editable
    g_form.setReadOnly('state', false);
}

 

MIftikhar_0-1761210352362.png

 

MIftikhar_1-1761210359294.png

 

Thanks & Regards,
Muhammad Iftikhar

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