- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 03:32 AM
I'm stuck in this script and cant seem to find the solution.
The field 'My Team' should only be visible and mandatory if 'Access Level' is manager. Otherwise, 'My Team' shouldn't even be visible.
How can i solve this? I'm aware its something simple but at this point i feel like I've everything
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 03:39 AM
you have mixed up your script
update the script as this
Make UI Type - ALL
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
if (newValue == "manager") { // give the correct value to compare
g_form.setDisplay('my_team', true);
g_form.setMandatory('my_team', true);
} else {
g_form.setMandatory('my_team', false);
g_form.setDisplay('my_team', false);
}
}
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
01-06-2025 03:46 AM
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setMandatory('my_team', false);
g_form.setDisplay('my_team', false);
return;
}
if (g_form.getValue('access_level') == 'manager') {
g_form.setMandatory('my_team', true);
g_form.setDisplay('my_team', true);
} else {
g_form.setMandatory('my_team', false);
g_form.setDisplay('my_team', false);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 03:39 AM
you have mixed up your script
update the script as this
Make UI Type - ALL
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
if (newValue == "manager") { // give the correct value to compare
g_form.setDisplay('my_team', true);
g_form.setMandatory('my_team', true);
} else {
g_form.setMandatory('my_team', false);
g_form.setDisplay('my_team', false);
}
}
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
01-06-2025 03:46 AM
Hello @NMMZ10 ,
Instead of creating any client script, you can do this by creating a UI Policy.
Note - My Team variable is hidden.
1. Create a new UI policy and add a condition as Access Level is manager.
2. Save the UI Policy.
3. Create a UI policy action under the same record.
4. Select variable name as My Team, and make Mandatory and Visible as True.
Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.
Thanks and Regards,
Ashish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 03:46 AM
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setMandatory('my_team', false);
g_form.setDisplay('my_team', false);
return;
}
if (g_form.getValue('access_level') == 'manager') {
g_form.setMandatory('my_team', true);
g_form.setDisplay('my_team', true);
} else {
g_form.setMandatory('my_team', false);
g_form.setDisplay('my_team', false);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 03:50 AM
Hi @NMMZ10,
As on onChange script runs onLoad as well, you can easily handle this within an onChange script.
Update the Script Type to onChange and select 'access level' as the variable.
Use the following script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
// Hide the "My Team" variable initially
g_form.setMandatory('my_team', false);
g_form.setDisplay('my_team', false);
}
if (newValue == 'manager') {
// Show and make "My Team" mandatory
g_form.setDisplay('my_team', true);
g_form.setMandatory('my_team', true);
} else {
// Hide and make "My Team" not mandatory
g_form.setMandatory('my_team', true);
g_form.setDisplay('my_team', true);
}
}
To help others (and for me to gain recognition for my efforts), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie