hello, I'm currently struggling with some issue, which I'm not sure whether that is a bug in ServiceNow or issue in my script logic. In general, I have a catalog item, which needs to load different variables in the UI, depending on the selected Request Type variable (of type choice). The problem is with some over-left variables from previous Request type selection (not cleared from the UI (not hidden) after changing selection in Request Type). And that happens after 2-3 changes of the request type. The first few selections are working fine. Initially tried with UI Policies, but that did not help. Then I created onChange client script, which is more flexible. I used reset of all fields (clear the value, remove mandatory and hide the field). I also used some timeout as well in order to give time for the page to refresh with proper fields, but that also didn't help. Also tried fields resetting inside each Case, as well timeouts inside each Case, but no success. Also tested in private session, cleared cookies without success. Has someone experienced such isssue? Here is the script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) return;
g_form.addInfoMessage('Please be informed, that the UI is not refreshing (show/hide) properly the fields after the Request type is changed frequently. If you notice such behaviour, please reload the page.')
// --- List all fields that could appear in any request type ---
var allFields = [
'new_security_group_name',
'are_you_the_owner',
'owner',
'owner1',
'owner2',
'list_member_s',
'security_group_name_ref',
'owner_s_to_remove'
];
// --- Helper to fully reset/hide all fields ---
function resetAll() {
allFields.forEach(function(field) {
if (g_form.hasField(field)) {
g_form.setDisplay(field, false);
g_form.setMandatory(field, false);
g_form.setReadOnly(field, false);
g_form.setValue(field, '');
}
});
}
// --- Step 1: reset everything ---
resetAll();
// --- Stop if no selection ---
if (!newValue) return;
// --- Step 2: show only the relevant fields for current selection ---
switch (newValue) {
case 'create_new_security_group':
g_form.setDisplay('new_security_group_name', true);
g_form.setMandatory('new_security_group_name', true);
g_form.setDisplay('are_you_the_owner', true);
g_form.setMandatory('are_you_the_owner', true);
// owner visibility handled separately
g_form.setDisplay('list_member_s', true);
g_form.setMandatory('list_member_s', true);
break;
case 'change_the_group_owner':
g_form.setDisplay('security_group_name_ref', true);
g_form.setMandatory('security_group_name_ref', true);
g_form.setDisplay('owner_s_to_remove', true);
g_form.setReadOnly('owner_s_to_remove', true);
g_form.setDisplay('owner2', true);
g_form.setMandatory('owner2', true);
break;
case 'add_members_to_security_group':
case 'remove_members_from_security_group':
g_form.setDisplay('security_group_name_ref', true);
g_form.setMandatory('security_group_name_ref', true);
g_form.setDisplay('list_member_s', true);
g_form.setMandatory('list_member_s', true);
break;
case 'delete_security_group':
g_form.setDisplay('security_group_name_ref', true);
g_form.setMandatory('security_group_name_ref', true);
break;
default:
// fallback: everything remains hidden
resetAll();
break;
}
}