please tell me script right or wrong , if wrong pls correct i
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
31. Prevent change to a specific value
Field: State
Script: if(newValue == 'Closed'){ alert('You cannot directly set state to Closed');
g_form.setValue('state', oldValue); }
Explanation: Enforces process rules.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hi @venkyc ,
if the state field value Closed is Closed then it will work but there is issue with your script it wont show the field message properly.
as per the your code it directly set the value not show any message under the 'state' field.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '' ||newValue === oldValue) {
return;
}
if (newValue === '7') { // '7' is the "Closed" state 'change the value '7' to 'Closed'
g_form.hideFieldMsg('state');
g_form.setValue('state', oldValue);
setTimeout(function() {
g_form.showFieldMsg('state', 'Cannot close directly. Please follow the proper workflow.', 'error');
}, 100);
}}
Thanks,
BK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
what's your business requirement?
you are restricting Closed state using onChange script
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
15m ago
Hello @venkyc ,
I just want to clarify which table this client script is applied to.
If this script is applied on the Incident table, then the Closed state has a backend value of 7, so the condition newValue == '7' should work for displaying the error message.
However, please note that Incident already has OOTB data policies that enforce mandatory fields like Close Notes and Close Code when moving the state to Closed.
If this script is being used on any other table, that table may have a different backend value for the Closed (or equivalent) state. In that case, you should verify the backend value of the state choice on that table and compare that value with newValue in the script.
