cLient script not working here i wrote script to make readonly when ticket moved to closed state.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hi @RAJPRAVEENR
I can see some isues in you code:
- Case sensitivity – In ServiceNow, g_form.getValue() has a capital V, not getvalue().
- getEditableFields() – There is no native g_form.getEditableFields() API. That will return undefined. Instead, you need to loop through the fields you want to lock, or use g_form.getElements() if you want all elements.
- Best practice – Instead of looping through everything, it’s better to explicitly list the fields you want read-only.
Here’s a fixed version of your script
function onLoad() {
var st = g_form.getValue('state'); // Capital V
if (st == 7) {
var fieldsToLock = ['short_description', 'description', 'category', 'priority']; // add all fields you need
for (var i = 0; i < fieldsToLock.length; i++) {
g_form.setReadOnly(fieldsToLock[i], true);
}
}
}
Now… If you really want to lock all fields without listing them, you can do:
function onLoad() {
var st = g_form.getValue('state');
if (st == 7) {
var elements = g_form.getEditableFields(); // Only available in newer UI
if (elements) {
for (var i = 0; i < elements.length; i++) {
g_form.setReadOnly(elements[i], true);
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
try this, I hope you are comparing correct choice value
function onLoad() {
var st = g_form.getValue('state');
if (st.toString() == '7') {
var fields = g_form.getEditableFields();
for (var x = 0; x < fields.length; x++) {
g_form.setReadOnly(fields[x], true);
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hi @RAJPRAVEENR
I see the issue 👀. The problem is small but important — in ServiceNow client scripts, methods are case-sensitive.
In your script:
It should be:
Here’s a working corrected version of your script:
Please accept my answer and mark as helpful/correct if it resolves your query.
Regards,
Nitish Saini
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hi @RAJPRAVEENR ,
try this
function onLoad() {
if (g_form.getvalue('state') == '7')
g_form.disable();
}
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya