Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to show hide option values with Client Script based on type and action

Ajay Singh2
Tera Contributor

Hello All,

 

I need to display option variables based on type = abc or xyz than action dropdown values will display based on type selection but currently its working first time but when I change type values than its only showing Remove option. Please see below code.

 

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

  if (g_form.getValue('type') == 'abc') {
    g_form.removeOption('action', 'Add New Access');

  }
    else if (g_form.getValue('type') == 'xyz')  {
    g_form.removeOption('action', 'Create New Access Group');
    g_form.removeOption('action', 'Modify Group Access');
    g_form.removeOption('action', 'Add User to Group');
    g_form.removeOption('action', 'Temporary Sudo to Root Access');
    g_form.removeOption('action', 'Modify Existing Access', 'Modify Existing Access');
  }

}
 
Thank You
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Ajay Singh2 

try this

1) clear all options

2) then add whatever options you want

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

  // First, clear all options
  g_form.clearOptions('action');

  // Add default options
  g_form.addOption('action', '', '--Select--');

  if (g_form.getValue('type') == 'abc') {
    g_form.addOption('action', 'Add New Access', 'Add New Access');
  } else if (g_form.getValue('type') == 'xyz') {
    g_form.addOption('action', 'Create New Access Group', 'Create New Access Group');
    g_form.addOption('action', 'Modify Group Access', 'Modify Group Access');
    g_form.addOption('action', 'Add User to Group', 'Add User to Group');
    g_form.addOption('action', 'Temporary Sudo to Root Access', 'Temporary Sudo to Root Access');
    g_form.addOption('action', 'Modify Existing Access', 'Modify Existing Access');
  }
}

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

View solution in original post

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@Ajay Singh2 

try this

1) clear all options

2) then add whatever options you want

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

  // First, clear all options
  g_form.clearOptions('action');

  // Add default options
  g_form.addOption('action', '', '--Select--');

  if (g_form.getValue('type') == 'abc') {
    g_form.addOption('action', 'Add New Access', 'Add New Access');
  } else if (g_form.getValue('type') == 'xyz') {
    g_form.addOption('action', 'Create New Access Group', 'Create New Access Group');
    g_form.addOption('action', 'Modify Group Access', 'Modify Group Access');
    g_form.addOption('action', 'Add User to Group', 'Add User to Group');
    g_form.addOption('action', 'Temporary Sudo to Root Access', 'Temporary Sudo to Root Access');
    g_form.addOption('action', 'Modify Existing Access', 'Modify Existing Access');
  }
}

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