top of page

Experiencing challenges with ServiceNow support?

IKC provides professional, reliable assistance that covers gaps not supported by ServiceNow

and without the high costs of traditional services.

 

Starting from just $1,000

Controlling the Order of Select Box Choices Using g_form.addOption in ServiceNow

Updated: Mar 29

Controlling the Order of Select Box Choices Using g_form.addOption in ServiceNow

Properly ordering options in Select Box variables can significantly enhance user experience on ServiceNow forms. Many administrators use the g_form.addOption method within Client Scripts to dynamically manage the choices available to users. However, administrators occasionally encounter unexpected behavior where the order of choices displayed does not match the order specified in their script. This article explains the cause of this issue and provides a clear, step-by-step solution to ensure Select Box choices are displayed in the intended order.


Why Are Choices Not Displaying in the Correct Order?

When using the g_form.addOption method in ServiceNow Client Scripts, administrators often mistakenly assume that choices will automatically appear in the order they are added to the script. However, this is not always the case. Without specifying an explicit order index for each choice, ServiceNow may display choices in an unexpected sequence.

For example, consider the following script snippet:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if(newValue == 'var_1' || newValue == 'var_2') {
        g_form.removeOption('var_3', 'choice_1_value');
		// Remove based on the value (not the label)
    } else {
        g_form.addOption('var_3', 'choice_1_value', 'Choice1');
        g_form.addOption('var_3', 'choice_2_value', 'Choice2');
    }
}

Administrators expect "Choice1" to appear first, followed by "Choice2". However, ServiceNow might display these options in a different order because no explicit index is provided.


Verified Solution to Ensure Proper Choice Order

To address this issue, ServiceNow's g_form.addOption method accepts an optional index parameter. This parameter explicitly defines the position of each choice within the Select Box. By adding this parameter, administrators can control exactly how choices appear.

Here is the corrected script with explicit index positions:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if(newValue == 'var_1' || newValue == 'var_2') {
        g_form.removeOption('var_3', 'choice_1_value'); 
		// Remove based on the value (not the label)
    } else {
        g_form.addOption('var_3', 'choice_1_value', 'Choice1', 1);
        g_form.addOption('var_3', 'choice_2_value', 'Choice2', 2);
    }
}

By assigning an explicit index value (such as 1 and 2 in the example), "Choice1" will reliably display first, followed by "Choice2."


Practical Example and Alternative Approaches

A common use case involves dynamically populating choices from a server-side script using GlideAjax. Even in such cases, including explicit indexing ensures accurate order.

Consider this practical example:

var ga = new GlideAjax('JobCodeRetriever');
ga.addParam('sysparm_name', 'getJobCodes');
ga.getXMLAnswer(function(response) {
    var jobCodes = JSON.parse(response);
    for (var i = 0; i < jobCodes.length; i++) {
        g_form.addOption('Job_code', jobCodes[i].value, jobCodes[i].label, i + 1);
		// The index starts from 1, so we use i + 1.

    }
});

This approach guarantees that choices retrieved from GlideAjax calls appear precisely in the desired sequence.

Alternatively, administrators can manage option order through form design settings or variables within ServiceNow, but explicit indexing via scripting provides the greatest flexibility.


Conclusion and Next Steps

To ensure Select Box choices display correctly, always include explicit indexing when using g_form.addOption in Client Scripts. This practice enhances user experience by clearly and consistently presenting options.

As a next step, review your existing Client Scripts to verify explicit indexing is implemented. Additionally, familiarize yourself with GlideAjax best practices for dynamic option loading to further optimize your ServiceNow forms.

Experiencing challenges with ServiceNow support?

IKC provides professional, reliable assistance that covers gaps not supported by ServiceNow

and without the high costs of traditional services.

 

Starting from just $1,000

CONTACT

New Zealand HQ

Integrated Knowledge Consulting Office

Level 3, 93 Grafton Road

Auckland

South Korea

Integrated Knowledge Consulting Office

BMY역삼타워 6층

서울특별시 강남구 역삼동 678-10번지

 

info@ikconsulting.com

Thanks for submitting!

  • LinkedIn Social Icon

© Copyright 2025 Integrated Knowledge Consulting. All rights reserved.

bottom of page