Unable to Hide Field when i Choose None Option
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2025 12:19 AM
I have created three choice include none, All my choices work as expected but when i choose choice 1 to None it fail to hide the filed and same for choice 2 and 3 as well kindly need clarity on this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2025 08:05 PM
Hi @rajeshraji1
You can acheive the sam eby using (catalog) UI policy to acheive your requirement without scripting.
Could you share more info on the requirement?
Regards,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2025 08:09 PM
try this script once
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return; // Prevent running during initial load
}
if (newValue === oldValue) {
return;
}
handleEmployeeTypeChange(newValue);
}
function handleEmployeeTypeChange(selectedType) {
// Field names
var employeeNameField = 'employee_name';
var employeeIdField = 'employee_id';
var traineeNameField = 'trainee_name';
var traineeIdField = 'trainee_id';
var studentNameField = 'student_name';
var studentIdField = 'student_id';
// Function to set field visibility and mandatory status
function setFieldDisplayAndMandatory(fieldName, visible, mandatory) {
g_form.setDisplay(fieldName, visible);
g_form.setMandatory(fieldName, mandatory);
}
// Initially hide and make all fields non-mandatory
setFieldDisplayAndMandatory(employeeNameField, false, false);
setFieldDisplayAndMandatory(employeeIdField, false, false);
setFieldDisplayAndMandatory(traineeNameField, false, false);
setFieldDisplayAndMandatory(traineeIdField, false, false);
setFieldDisplayAndMandatory(studentNameField, false, false);
setFieldDisplayAndMandatory(studentIdField, false, false);
// Show and set mandatory based on the selected choice
if (selectedType === 'none' || selectedType === '') {
// Hide all fields
setFieldDisplayAndMandatory(employeeNameField, false, false);
setFieldDisplayAndMandatory(employeeIdField, false, false);
setFieldDisplayAndMandatory(traineeNameField, false, false);
setFieldDisplayAndMandatory(traineeIdField, false, false);
setFieldDisplayAndMandatory(studentNameField, false, false);
setFieldDisplayAndMandatory(studentIdField, false, false);
} else if (selectedType === 'newemployee') {
setFieldDisplayAndMandatory(employeeNameField, true, true);
setFieldDisplayAndMandatory(employeeIdField, true, true);
} else if (selectedType === 'trainee') {
setFieldDisplayAndMandatory(traineeNameField, true, true);
setFieldDisplayAndMandatory(traineeIdField, true, true);
} else if (selectedType === 'student') {
setFieldDisplayAndMandatory(studentNameField, true, true);
setFieldDisplayAndMandatory(studentIdField, true, true);
}
}
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
04-27-2025 09:27 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2025 01:28 AM
Not working