- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2025 12:36 PM - edited ‎07-24-2025 12:58 PM
We have a variable set named "tasks", and all variables within this set are currently marked as mandatory. This variable set is only displayed in the variable editor section of the sc_task form.
Now, the requirement is:
The variables in the "tasks" variable set should only be mandatory when the state field on the sc_task form is set to Closed Complete.
How can we implement this logic effectively, state is a field on the sc_task table and not part of the variable set?
I tried with standard UI policy, but it is making whole variable set mandatory as we have variables visibility on conditions.
For example we have variable - How many tasks required?
Options- 1. One Task, 2. Two tasks, 3. Three tasks.
If user select one task, assignment group one get visible
if user select two tasks , assignment group one and assignment group two get visible
But Using this UI policy all variables are visible on form load.
Script-
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2025 11:36 PM
2 ways
1) normal onChange client script on state field of sc_task and check if state is closed complete then make those mandatory
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue == '3') {
g_form.setMandatory('variables.variableName1', true);
g_form.setMandatory('variables.variableName2', true);
g_form.setMandatory('variables.variableName3', true);
}
}
2) You need to ensure this is handled via the OOTB Close Task UI action as well
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
‎07-24-2025 11:23 PM
Hi,
Try to use g_form.variables.variable_name
function onCondition() {
if (g_form.getValue('variables.no_of_tasks') == '1') {
g_form.setMandatory('variables.task_1', true);
g_form.setMandatory('variables.group_1', true);
} else if (g_form.getValue('variables.no_of_tasks') == '2') {
g_form.setMandatory('variables.task_1', true);
g_form.setMandatory('variables.group_1', true);
g_form.setMandatory('variables.task_2', true);
g_form.setMandatory('variables.group_2', true);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2025 11:36 PM
2 ways
1) normal onChange client script on state field of sc_task and check if state is closed complete then make those mandatory
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue == '3') {
g_form.setMandatory('variables.variableName1', true);
g_form.setMandatory('variables.variableName2', true);
g_form.setMandatory('variables.variableName3', true);
}
}
2) You need to ensure this is handled via the OOTB Close Task UI action as well
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
‎07-29-2025 02:35 AM - edited ‎07-29-2025 02:47 AM
I tried the script and it's working fine.
Issue is - when I'm trying to change - "How many task required" variable field-
Old value - one task and new value - two tasks,
It's not making mandatory for two tasks as shown in SS
Script-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2025 02:51 AM
this is other issue and doesn't relate to your main requirement where you wanted to make variables mandatory on task closure
I believe I already shared answer to your main 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