Client Script to show values based on selection

HP8
Giga Guru

I know this topic has been asked a lot, but I have a specific set of requirements. I cannot use field dependencies because our category choices are used on more than one occassion.

 

Category field needs to display certain choices based on selection from our Product field, however it is not working.

 

Current client script setup:

 

Type: onChange

Field: Product

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	if(g_form.getDisplayBox('product').value == 'Test Product'){
		g_form.clearOptions('category');
		g_form.addOption('category','666','Test Category');
		}
}

 

 

The Product field is a reference field to the cmdb_model:

Table: sn_customerservice_case
Field: product
Type: reference
Reference: cmdb_model

 

Category field an is out of the box integer field.

2 REPLIES 2

Vishal Birajdar
Giga Sage

Hi @HP8 

 

Can you try with updated line of code.

 

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

              if (isLoading || newValue === '') {

                        return;

                  }

             

                 if(g_form.getDisplayValue('product') == 'Test Product'){

                                   g_form.clearOptions('category');

                                   g_form.addOption('category','666','Test Category');

                  }else {

                      // add options if value is different 

                          }

}

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Satyapriya
Mega Sage

Hi @HP8

Can you try this one 

if (newValue == 'product_value') {

        g_form.clearOptions('category');

        g_form.addOption('category''666''Test Category');

    }