- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
@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?
