Difference between g_form.clearValue() and g_form.clearOptions() for Catalog Item variables

AzeemullahA
Tera Contributor

Hi Team, I am working on a ServiceNow Catalog Item where I am trying to understand the difference between clearing a selected value and clearing available choices in a catalog variable. I want to know when a selected value should be cleared, when available choices should be removed, whether removing choices works for multiple choice radio button variables, and what is the recommended approach if a selected option becomes invalid due to another variable change. Any guidance or best practice would be helpful. Thanks.

2 ACCEPTED SOLUTIONS

miftikhar20
Kilo Patron

Hi @AzeemullahA,

 

Here is the key differences:

ActionAPI MethodWhat it doesWhen to use it
Clear Valueg_form.clearValue('var_name')Wipes the current selection/input but leaves the dropdown options intact.When the current input is no longer valid, but the user should still be able to pick from the same list.
Clear Optionsg_form.clearOptions('var_name')Removes all selectable entries from a Choice list or Lookup variable.When the "parent" variable changes, making the previous list of options completely irrelevant.

 

If my response helped, please mark it as the accepted solution so others can benefit as well.

 

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.

View solution in original post

DiveshTyagi
Mega Guru

Hi @AzeemullahA ,

 

 

Great question — this comes up often when building dynamic catalog items. The distinction between clearing a selected value and clearing available choices is important.

 

The difference is:

  • Clear selected value → resets the user’s current choice (sets the variable to blank).
  • Clear available choices → removes options from the list so the user cannot pick them.
  • For multiple choice / radio button variables, both actions apply. If you remove a choice that was selected, you should also clear the selected value to avoid invalid submissions.

-----------------------------------------------------------------------------------------------------------------------------------------------

 Best Practice:

  • When another variable changes and makes a choice invalid:
  1. Remove invalid choices from the available list.
  2. Clear the selected value if the user had already chosen one of those invalid options.

This ensures the catalog item stays consistent and prevents bad data from being submitted.

// Client Script: onChange of "region" variable
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    // Reference to the dependent variable
    var modelVar = g_form.getField('model');

    // Clear all available choices first
    g_form.clearOptions('model');

    // Add back only valid choices based on region
    if (newValue === 'Europe') {
        g_form.addOption('model', 'A', 'Model A');
        g_form.addOption('model', 'B', 'Model B');
    } else if (newValue === 'Asia') {
        g_form.addOption('model', 'C', 'Model C');
    }

    // If the previously selected value is no longer valid, clear it
    var currentSelection = g_form.getValue('model');
    if (!g_form.hasOption('model', currentSelection)) {
        g_form.clearValue('model');
    }
}

 

What this does

  • clearOptions('model') → removes all available choices.
  • addOption() → adds back only the valid ones.
  • clearValue('model') → clears the selected value if it’s no longer valid.

 

 

 

--------------------------------------------------------------------------------------------------------------------------------------------

If my response helped, please mark it as the accepted solution so others can benefit as well.

View solution in original post

4 REPLIES 4

SumanthDosapati
Mega Sage

@AzeemullahA 

 

g_form.clearValue() is to clear the filled/selected value (usually for all variables including single line text and choices type of variables)

g_form.clearOptions() is to remove all available options (for variables like select box)

g_form.removeOption() is to make a particular choice unavailable in the dropdown or radio button etc

 

Accept the solution and mark as helpful if it does, to benefit future readers.
Regards,
Sumanth

miftikhar20
Kilo Patron

Hi @AzeemullahA,

 

Here is the key differences:

ActionAPI MethodWhat it doesWhen to use it
Clear Valueg_form.clearValue('var_name')Wipes the current selection/input but leaves the dropdown options intact.When the current input is no longer valid, but the user should still be able to pick from the same list.
Clear Optionsg_form.clearOptions('var_name')Removes all selectable entries from a Choice list or Lookup variable.When the "parent" variable changes, making the previous list of options completely irrelevant.

 

If my response helped, please mark it as the accepted solution so others can benefit as well.

 

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.

Gaurav Vaze
Kilo Sage

example:

1. Example: g_form.clearValue

Use this when you want to "blank out" a field because it is no longer relevant.

JavaScript
 
// Scenario: If Category is 'Hardware', clear the 'Software License' reference field
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    if (newValue == 'hardware') {
        g_form.clearValue('u_software_license'); // The field stays on form, but the text is wiped
    }
}

2. Example: g_form.clearOptions

Use this when you need to dynamically change what a user is allowed to pick.

JavaScript
 
// Scenario: Dynamically update Subcategory options based on Category
function onChange(control, oldValue, newValue, isLoading) {
    g_form.clearOptions('subcategory'); // Wipes the list clean (even -- None --)
    g_form.addOption('subcategory', '', '-- None --'); // Re-add default empty option

    if (newValue == 'software') {
        g_form.addOption('subcategory', 'os', 'Operating System');
        g_form.addOption('subcategory', 'email', 'Email / Outlook');
    } else if (newValue == 'hardware') {
        g_form.addOption('subcategory', 'monitor', 'Monitor');
        g_form.addOption('subcategory', 'laptop', 'Laptop');
    }
}

DiveshTyagi
Mega Guru

Hi @AzeemullahA ,

 

 

Great question — this comes up often when building dynamic catalog items. The distinction between clearing a selected value and clearing available choices is important.

 

The difference is:

  • Clear selected value → resets the user’s current choice (sets the variable to blank).
  • Clear available choices → removes options from the list so the user cannot pick them.
  • For multiple choice / radio button variables, both actions apply. If you remove a choice that was selected, you should also clear the selected value to avoid invalid submissions.

-----------------------------------------------------------------------------------------------------------------------------------------------

 Best Practice:

  • When another variable changes and makes a choice invalid:
  1. Remove invalid choices from the available list.
  2. Clear the selected value if the user had already chosen one of those invalid options.

This ensures the catalog item stays consistent and prevents bad data from being submitted.

// Client Script: onChange of "region" variable
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    // Reference to the dependent variable
    var modelVar = g_form.getField('model');

    // Clear all available choices first
    g_form.clearOptions('model');

    // Add back only valid choices based on region
    if (newValue === 'Europe') {
        g_form.addOption('model', 'A', 'Model A');
        g_form.addOption('model', 'B', 'Model B');
    } else if (newValue === 'Asia') {
        g_form.addOption('model', 'C', 'Model C');
    }

    // If the previously selected value is no longer valid, clear it
    var currentSelection = g_form.getValue('model');
    if (!g_form.hasOption('model', currentSelection)) {
        g_form.clearValue('model');
    }
}

 

What this does

  • clearOptions('model') → removes all available choices.
  • addOption() → adds back only the valid ones.
  • clearValue('model') → clears the selected value if it’s no longer valid.

 

 

 

--------------------------------------------------------------------------------------------------------------------------------------------

If my response helped, please mark it as the accepted solution so others can benefit as well.