Client Script to Hide a option

Sohini Kar
Tera Expert

I have a variable "Action Required" which has two options: Add and Remove.

The requirement is: When we select Add, Option "New website access for CUI secure transfer" should be visible. And when we select Remove, that option should not be visible.

 

I wrote a client script, but When I am selecting Add, the option is showing.

When I am selecting Remove, The option is not showing.

 

But again when I am selecting Add, the option is still not showing unless I refresh the page. Kindly let me know how to fix this issue. Thanks.

 

Field Name: Type of Request

Backend value: dependent_field_1

 

I wrote the below OnChange Client Script:

 

function onChange(control, oldValue, newValue, isLoading) 
{
   if (isLoading || newValue == '1') 
   {
      g_form.addOption('dependent_field_1','New website access for CUI secure transfer');
   }
 else 
 {
          g_form.removeOption('dependent_field_1','New website access for CUI secure transfer');
                  }
   } 

 

 

 

1 ACCEPTED SOLUTION

Prince Arora
Tera Sage
Tera Sage

@Sohini Kar 

 

can you try below script:

 

function onChange(control, oldValue, newValue, isLoading) 
{
if (isLoading || newValue == '') 
{
return;
}
if(newValue=="1"){
g_form.addOption('dependent_field_1','New website access for CUI secure transfer',"New website access for CUI secure transfer");
}

else 
{
g_form.removeOption('dependent_field_1','New website access for CUI secure transfer');
}

 

 

View solution in original post

7 REPLIES 7

Karan Chhabra6
Mega Sage
Mega Sage

Hi @Sohini Kar ,

 

This might be because of 'isLoading' which is evaluating to false

Please try this client script and test again

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

    if (newValue == '1') {
        g_form.addOption('dependent_field_1', 'New website access for CUI secure transfer');
    } else {
        g_form.removeOption('dependent_field_1', 'New website access for CUI secure transfer');
    }

}

 

If my answer has helped with your question, please mark it as correct and helpful

 

Thanks!

Same issue: 

 When I am selecting Add, the option is showing.

When I am selecting Remove, The option is not showing.

 

But again when I am selecting Add, the option is not showing unless I refresh the page. 

@Sohini Kar 

please use the correct syntax for addOption

you need to mention the choice label and choice value both

Removing or Disabling Choice List Options 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Prince Arora
Tera Sage
Tera Sage

@Sohini Kar 

 

can you try below script:

 

function onChange(control, oldValue, newValue, isLoading) 
{
if (isLoading || newValue == '') 
{
return;
}
if(newValue=="1"){
g_form.addOption('dependent_field_1','New website access for CUI secure transfer',"New website access for CUI secure transfer");
}

else 
{
g_form.removeOption('dependent_field_1','New website access for CUI secure transfer');
}