How to add option, remove option, get display Value, Clear Option in Choice List?

ServiceNow choice lists in ServiceNow platform is used for creating drop down list. Selectbox/choice list provides a predefined set of values from which user can select when filling out forms or configuring records.

Choice lists are primarily used to provide predefined options for fields in tables such as incident categories, priority levels, state transitions and more. ServiceNow Choice Lists can be associated with various data types, including string, integer, Boolean and more.

Below ServiceNow Choice list we are going to cover in this blog along with practical demonstration:

  • How to create Selectbox/Choice list in a ServiceNow form and how to add options to it?
  • How to remove option and add option in Selectbox?
  • How to get value from Selectbox?
  • How to get Display value of an option from Selectbox?
  • How to clear all values of choice list?
  • Code used in YouTube video for practical implementation of use case scenario.

How to create Selectbox/Choice list in a ServiceNow form and how to add options to it?

Below is the practical demontration link to create the choice list in the form:

How to remove option and add option in Selectbox?

To remove option from the choicelist, we can use below syntax:
g_form.removeOption(g_form.removeOption('variablename','choicelistvalue');
To add option from the choicelist, we can use below syntax:
g_form.addOption('variablename','value','optiondisplayvalue',indexnumber);

How to get value from Selectbox?

Below syntax we can use to get value from the selectbox:
g_form.getValue('variablename');

How to get Display value of an option from Selectbox?

We can use below syntax to get display value of the choice list:
g_form.getOption('category','value').text;

How to clear all the options from the choice list or Selectbox?

Below is the syntax to clear all the values or option of the choice list:
g_form.clearOptions('variablename');

Code used in YouTube video for practical implementation of use case scenario.

The Use Case Scenario is mentioned below:
  • Create Selectbox name “Impacted Item” in incident form. Selectbox should have below options:
  • Outlook, Teams, Laptop, Desktop, SQL, Oracle, VPN
  • When user Select Category as Software then “Laptop” and “Desktop” option should be removed from “Impacted Item” Selectbox and when user select category as hardware then “Outlook” and “Team” options should be removed” from the Selectbox.
  • Alert message to display options selected in “Category” choice list.

Code:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
var currentCategory=g_form.getOption('category',g_form.getValue('category')).text;
// Display alert message and display value of category choicelist when user select category
alert(" The current selected category is :  "+ currentCategory);
// when the selected category is software
if(g_form.getValue('category')=='software'){
g_form.removeOption('u_impacted_item','3');
g_form.removeOption('u_impacted_item','4');
g_form.addOption('u_impacted_item','1','Outlook',1);
g_form.addOption('u_impacted_item','2','Teams',2);
}
// when the selected category is hardware
else if(g_form.getValue('category')=='hardware'){
g_form.removeOption('u_impacted_item','1');
g_form.removeOption('u_impacted_item','2');
g_form.addOption('u_impacted_item','3','Laptop',1);
g_form.addOption('u_impacted_item','4','Desktop',2);
}
// When the category is not software or Hardware
else{
g_form.addOption('u_impacted_item','1','Outlook',1);
g_form.addOption('u_impacted_item','2','Teams',2);
g_form.addOption('u_impacted_item','3','Laptop',3);
g_form.addOption('u_impacted_item','4','Desktop',4);
}
   
}

ServicNow Choice List Examples, how to add option in choice list, how to remove option from servicenow choice list, how to get the display value of selectbox option

We hope all the information and scenario mentioned above related to ServiceNow choice list or Selectbox is helpful for you. If you have any questions related to any of the scenario please post the same in below comment section or you can email the same to us. Thankyou !!!

No comments:

Thankyou !!!!

Powered by Blogger.