Client Script not working in SOW
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2025 01:16 PM
Hey all - I did read through some other posts but didn't find what I was looking for as I had already done those steps of troubleshooting.
I am working on rescoping some of our work as our SOW was stood up quickly and before there was much in docs/support. We have a 23 action UI Policy that I was able to rewrite as a Client Script because that seemed like a cleaner option. That runs as expected in Legacy (eventually we will be shutting Legacy off so trying to also get ahead of any issues that doing that may cause). For the SOW I cannot get a very similar script to run nor can I recreate the UI Policy in Form/UI Builder for 23 actions - the limit is 14 (I also opened a Case for this). I haven't chosen a View as that doesn't have a drop down (also bizarre) so left it blank but have tried typing in sow, SOW, a and Service Operations Workspace with no luck. Here is the code and screenshots. I appreciate any ideas that I haven't mentioned trying already. Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2025 10:11 AM
I had a typo in my response above, I could NOT get a helper function to work in the onChange client script. (my posts are moderated so I couldn't edit it.
Here's a script to use in an onLoad client script.
function onLoad() {
// Adjust if your instance uses different values
var RESTORED = '6'; // "Resolved/Restored"
var CLOSED = '7';
var stateVal = g_form.getValue('state'); // added since function is gone
var shouldLock = (stateVal == RESTORED || stateVal == CLOSED);
// Only the fields you actually want to lock in these states
var fieldsToLock = [
// List of fields to make read-only
'state',
'opened_at',
'opened_by',
'closed_by',
'closed_at',
'company',
'caller_id',
'impact',
'urgency',
'location',
'number',
'cmdb_ci',
'escalation',
'assignment_group',
'assigned_to',
'short_description',
'priority',
'close_notes',
'closed_code',
'comments',
'knowledge',
'work_notes'
];
// If you need any to remain editable even when locked, put them here
var exceptions = [
'category'
];
// Diagnostics banner (remove when done)
// the follows doesn't work un-declared variables
g_form.addInfoMessage(
// '[LockCheck ' + (source || 'n/a') + '] state=' + stateVal +
// ' (' + (stateLabel || '?') + '), lock=' + shouldLock
'state=' + stateVal + ', lock=' + shouldLock
);
// Lock only if the control exists on the form (prevents silent no-ops)
for (var i = 0; i < fieldsToLock.length; i++) {
var f = fieldsToLock[i];
if (exceptions.indexOf(f) === -1) {
var ctrl = g_form.getControl(f);
if (ctrl) {
g_form.setReadOnly(f, shouldLock);
g_form.addInfoMessage('setting ' + f + ' ' + shouldLock);
} else {
// Log missing control so you can correct field names quickly
console && console.warn && console.warn('[LockCheck] Control not on form:', f);
g_form.addInfoMessage('[LockCheck] Not on form: ' + f);
}
}
}
}Two client scripts were needed to get your desired behavior.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2025 10:25 AM
There is also an OOB UI Policy defined on the incident table that interferes with the onLoad client script. it is named 'Make fields read-only on close' so the onLoad client script is not necessary OOB.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2025 09:58 PM
HI @LavaniaB
Declarative Form Configuration
Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help us a lot.
Thanks & Regards
Deepak Sharma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2025 12:08 AM
So what's the issue?
Is that not working?
💡 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