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.

3 REPLIES 3

SumanthDosapati
Mega Sage

@AzeemullahA 

 

g_form.clearValue() is to clear the filled value (usually for string variables like single line text. can be used for other variable types also)

g_form.clearOptions() is to clear the selected option (for variables like select box, radio buttons etc)

g_form.removeOption() is to make a 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

M Iftikhar
Tera Sage

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');
    }
}