please tell me script right or wrong , if wrong pls correct i
Options
- 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.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === oldValue) {
return;
}
if (newValue === 'Closed') {
g_form.showFieldMsg('state','Cannot close directly. Please follow the proper workflow.' );
g_form.setValue('state', oldValue);
}
2 REPLIES 2
Options
- 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.
g_form.showFieldMsg('state','Cannot close directly. Please follow the proper workflow.' );
g_form.setValue('state', oldValue);
This also applies when using g_form.setValue(). g_form.showFieldMsg() must come after the call to g_form.setValue() for the same field.
if you want to show the field message try something like this
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);
}}
If this information proves useful, kindly mark it as helpful or accepted solution.
Thanks,
BK
Thanks,
BK
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
34m ago
what's your business requirement?
you are restricting Closed state using onChange script
Regards,
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
