- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2025 03:19 AM - edited 06-27-2025 03:22 AM
Hi all ,
I have requirement where
when i open a new change task record default state is pending (-5)
so when its in pending state i want to hide close state (3).
and after i save the form then when i change the state to any other choice like open , cancelled or in progress
then close should appear back again ( both choices are in state field )
for this I wrote on load and on change client script:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2025 07:45 AM
Hope you are doing good.
Did my reply answer your question?
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
06-27-2025 03:53 AM
Hello @tushar_ghadage,
- Name: Show Close State on State Change
- Table: Change Task
- Advanced: True
- When to run: After
- Insert: True
- Update: True
- Condition:
current.state.changes()
(This ensures the rule runs only when the state field is updated) - Script:
(function() {
if (current.state.getDisplayValue() != 'Pending') {
var closeState = g_form.getControl('close_state_field'); //Replace with your actual field name for the close state.
if (closeState) {
closeState.style.display = '';
}
// Or if using a choice list, you can use:
// g_form.setDisplay('close_state_field', true); //Replace with your actual field name for the close state.
}
})();
If this is helpful, please hit the thumbs up button and accept the correct solution by referring to this solution in future it will be helpful to them.
Thanks & Regards,
Abbas Shaik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2025 04:59 AM
Are you sure this script will work?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2025 05:06 AM
try this
1) create onLoad client script and write a helper function and the same can be invoked from onchange client script as well
Note: use correct state choice value & label to add
onLoad client script
function onLoad() {
updateStateChoices(g_form.getValue('state'));
g_form.setMandatory('state', true); // Optional: make state mandatory
}
function updateStateChoices(stateValue) {
if (!g_form.getOption('state', '3')) {
g_form.addOption('state', '3', 'Closed');
}
if (stateValue == '-5') {
g_form.removeOption('state', '3');
}
}
onChange of state
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) return;
updateStateChoices(newValue);
}
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
06-29-2025 06:52 AM
Hope you are doing good.
Did my reply answer your question?
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