Incident Form - Resolution Code choices visibility

AakankshaSK
Tera Contributor

Hello Community,

 

I have a very basic requirement, but it seems I am not getting the required output even after multiple trials.

I have a field called 'Resolution code' (close_code) on the Incident form. It has 8 options. My requirement was to hide the options 5-8 when the incident state is not canceled. So, whenever someone opens a new or existing record, these options should remain hidden. This was achieved properly by using onLoad Client script.

 

Now the main issue is because of requirement no. 2. When the state change to Canceled in real time, the options 5-8 should get visible and the options 1-4 should now get hidden/removed from the list. I have tried multiple script to achieve this but to no avail. It is getting stuck at a point.

 

Kindly share your expertise in resolving this issue. Thank you for your guidance and support on this. Much appreciated.

4 REPLIES 4

Dr Atul G- LNG
Tera Patron

Hi @AakankshaSK 

 

https://www.servicenow.com/community/virtual-agent-forum/show-hide-state-field-choices-on-the-form/m...

 

*************************************************************************************************************
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/dratulgrover [ Connect for 1-1 Session]

****************************************************************************************************************

Dr Atul G- LNG
Tera Patron

https://www.servicenow.com/community/product-launch-forum/show-or-hide-on-the-variable-on-the-basis-...

*************************************************************************************************************
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/dratulgrover [ Connect for 1-1 Session]

****************************************************************************************************************

AnkaRaoB
Mega Guru

Hi @AakankshaSK ,

 

Choice list options are cached on the client, so hiding options only on form load is not enough. When the State field changes in real time, the Resolution Code list does not automatically refresh.

To handle this properly, you must clear and rebuild the choice list whenever the form loads and whenever the State changes.

 Recommended Solution

Use onLoad + onChange Client Scripts and dynamically rebuild the choices using g_form.clearOptions() and g_form.addOption().

 Step 1: onLoad Client Script

Table: Incident

Type: onLoad

function onLoad() {

    updateCloseCodeChoices();

}

 Step 2: onChange Client Script (on State)

Table: Incident

Field: state

Type: onChange

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading) return;

    updateCloseCodeChoices();

}

 Step 3: Shared Function (UI Script or Global Client Script)

function updateCloseCodeChoices() {

    var state = g_form.getValue('state');

 

    // Update with your actual choice values

    var activeChoices = ['1','2','3','4'];

    var cancelChoices = ['5','6','7','8'];

    // Clear existing options

    g_form.clearOptions('close_code');

    // Check Canceled state value in your instance

    if (state == '7') { // Example: 7 = Canceled

        // Show only cancel-related choices

        cancelChoices.forEach(function(val) {

            g_form.addOption('close_code', val, val);

        });

    } else {

        // Show only active choices

        activeChoices.forEach(function(val) {

            g_form.addOption('close_code', val, val);

        });

    }

    // Remove invalid existing value

    var currentValue = g_form.getValue('close_code');

    if ((state == '7' && activeChoices.includes(currentValue)) ||

        (state != '7' && cancelChoices.includes(currentValue))) {

        g_form.clearValue('close_code');

    }

}

Please note:

  Always use choice values, not labels.

Verify the actual value of the Canceled state in:

 

If this helps you then mark it as helpful and accept it as solution.

Ankur Bawiskar
Tera Patron

@AakankshaSK 

use 2 client scripts and it will help you

-> onLoad to hide choices based on state 

function onLoad() {
    var state = g_form.getValue('state');
    if (state != '8') { // Not Canceled (adjust value if needed)

        // also give proper choice values to remove
        g_form.removeOption('close_code', '5');
        g_form.removeOption('close_code', '6');
        g_form.removeOption('close_code', '7');
        g_form.removeOption('close_code', '8');
    }
}

-> onChange of state to handle real-time visibility

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    // Clear all options first for reset
    g_form.clearOptions('close_code');

    if (newValue === '8') { // Canceled state
        // Show all (1-8); options auto-repopulate from sys_choice
        // Optionally re-add specific if needed, but clearOptions + visibility handles it
    } else {
        // Hide 5-8 (1-4 remain)
        setTimeout(function() {
            g_form.removeOption('close_code', '5');
            g_form.removeOption('close_code', '6');
            g_form.removeOption('close_code', '7');
            g_form.removeOption('close_code', '8');
        }, 100); // Delay ensures options load post-clear
    }
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader