Category field displays the value instead of the category name

Mohamed Elsayed
Tera Expert

Hi Everyone,

 

I needed to make the category field options dependent on the Services field. The team suggested using the dependent field, which works well.

MohamedElsayed_1-1722520591786.png

However, I have some categories that need to depend on multiple services, which the dependent field doesn’t support. So, I created the following Client Script only for these categories which depend on multiple services:

 

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

    //Type appropriate comment here, and begin script below
    g_form.getReference('business_service', function(reference) {
        if (reference) {
            var serviceName = reference.name;
            var validServices = ['AS HCI OS', 'AKS Hybrid', 'Windows Server Support'];
            if (!validServices.includes(serviceName)) {
                g_form.addOption('category', 10, 'Category label 1');
                g_form.addOption('category', 20, 'Category label 2');
                g_form.addOption('category', 30, 'Category label 3');
                g_form.addOption('category', 40, 'Category label 4');
                g_form.addOption('category', 50, 'Category label 5');
                g_form.addOption('category', 60, 'Category label 6');
                g_form.addOption('category', 70, 'Category label 7');
                g_form.addOption('category', 80, 'Category label 8');
                g_form.addOption('category', 90, 'Category label 9');
            }
        } else {
            g_form.addErrorMessage('Unable to retrieve business service reference.');
        }
    });
}

 

Currently, I have a mix of categories: some depend on the dependent field and work without issues, while others rely on the script. The script functions correctly, but after selecting the service and its category and saving the form/case, the category field shows the value instead of the category name. Any suggestions on how to resolve this?

MohamedElsayed_0-1722520553369.png

 

2 ACCEPTED SOLUTIONS

Nicholas_Gann
Mega Guru

The display value for a choice field isn't stored on the record, only the actual value, so when the form is reloaded it will revert to the only piece of information it has, which is the value. It seems this is the issue as ServiceNow doesn't know where to get the display value from.

 

I've played about with dependent fields and the only obvious way I can think of to get it to show the display value on form load (without having an onLoad Client Script re-populate it) is to make the choice a valid option (taking into account it's dependent value).

 

You would need to make the above options in your script valid choices for 'category', which means you would need to remove these options so they are not selectable when they shouldn't be (when service is not A, B or C). You could make this choices inactive to prevent having to do this but then you would lose out on your ability to easily report on it (which i'm assuming is a problem you already have today)

 

EDIT: I see you have the choices already but the condition in your script is is not one of the 3 services, depending on the amount of services you have it's likely not feasible, so it's probably not possible

View solution in original post

smarti37
Kilo Sage

Hello Mohamed,

 

All the choice values you tried to manipulate via onChange Client Script should exit in the Choice table.

 

Also, to keep consistency, it's better to base all the mechanism on the "Dependent value" field and duplicate the relationships between "Services" and "Categories" in the Choice table => no script require and no complexity added to your configuration => you just have to manage Choice List and Dependent values.

 

This approach will also works in List view when trying to modify Service and Category field (similar mechanism of Category & Subcategory fields for Incident)

View solution in original post

10 REPLIES 10

Hi Nicholas,

Yes, all the options mentioned in the script are valid choices for the ‘category’ field. The options I am displaying using the client script do not have any dependent values.

 

 

In the test environment, I followed your suggestion and deactivated one of the categories options. The issue disappeared when I selected this option (it shows the name not the value), but I can’t rely on this solution as it would cause problems during reporting.

Ayushi12
Mega Sage

Hi @Mohamed Elsayed 

For addOption, the appropriate format is:

g_formaddOption(<fieldName>, <choiceValue>, <choiceLabel>, <targetIndex>); 

g_form.addOption('var_3', 'choice_1', 'Choice1', 1);

 

If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful."

Thanks!

smarti37
Kilo Sage

Hello Mohamed,

 

All the choice values you tried to manipulate via onChange Client Script should exit in the Choice table.

 

Also, to keep consistency, it's better to base all the mechanism on the "Dependent value" field and duplicate the relationships between "Services" and "Categories" in the Choice table => no script require and no complexity added to your configuration => you just have to manage Choice List and Dependent values.

 

This approach will also works in List view when trying to modify Service and Category field (similar mechanism of Category & Subcategory fields for Incident)

 

Hi Smarti,

 

All the choice values mentioned in the onChange Client Script are already present in the Choice table.

 

As you said, I would prefer to maintain consistency by using only the dependent field. However, it does not support one-to-many relationships. When you suggest duplicating the relationships between “Services” and “Categories” in the Choice table, I understand this to mean creating the same category option multiple times (e.g., five times) to link it to different services. This could be a viable solution, but I’m unsure how it would affect reporting.

Ehab Pilloor
Mega Sage

Hi @Mohamed Elsayed,

You need to add the field values in quotes, otherwise I feel the script will take it as default value.

 

 If you found my response helpful, please mark it as solution and helpful.

 

Thanks and Regards,

Ehab