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

Client Script not working in SOW

LavaniaB
Tera Contributor

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.

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

applyLockIfNeeded(newValue);
}
// shared helper for here and an onLoad()
function applyLockIfNeeded(stateVal) {
// Adjust if your instance uses different values
var RESTORED = '6'; // "Resolved/Restored"
var CLOSED = '7';
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)
g_form.addInfoMessage(
'[LockCheck ' + (source || 'n/a') + '] state=' + stateVal +
' (' + (stateLabel || '?') + '), 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);
} 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);
}
}
}
} catch (e) {
// Surface any JS error; otherwise Workspace swallows it and nothing runs
g_form.addErrorMessage('Locking script error: ' + e.message);
console && console.error && console.error('Locking script error', e);
}
3 REPLIES 3

WillieW
Tera Contributor

I tested a On-Change Client Script, for the State field on the incident table, with your code. It seems to work in the Service Operations Workspace.

 

On the Client script, I have the 'UI Type' set to "All" and I commented-out your debug and 'catch', the script follows

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
//	applyLockIfNeeded(newValue);
//}

  // shared helper for here and an onLoad()
//function applyLockIfNeeded(stateVal) {
	// Adjust if your instance uses different values
	var RESTORED = '6'; // "Resolved/Restored"
	var CLOSED = '7';
	var stateVal = newValue;		//	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
	);
	*/

	// 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);
			}
		}
	}
/*
	} catch (e) {
		// Surface any JS error; otherwise Workspace swallows it and nothing runs
		g_form.addErrorMessage('Locking script error: ' + e.message);
		console && console.error && console.error('Locking script error', e);
	}
*/
}

  I got the field to show as read-only once the state value was changed.

Deepak Shaerma
Kilo Sage
Kilo Sage

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

Ankur Bawiskar
Tera Patron
Tera Patron

@LavaniaB 

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! 🙏

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