The CreatorCon Call for Content is officially open! Get started here.

Populate choices based on reference field

Snehitha1
Tera Contributor

I have a requirement on knowledge article form.

IMG_20230402_225948__01.jpg

  • When knowledge base is hr general knowledge, category (kb_category - mandatory one in the picture) is not empty and topic field is general, the second category field in the picture should be mandatory and populate 5 choices

Please suggest.

Thank you.

 

1 ACCEPTED SOLUTION

Daniel Biesiada
Mega Guru

 

To implement this requirement in ServiceNow, you can create a client script that listens for changes in the "Knowledge Base," "Category," and "Topic" fields, and sets the behavior of the second "Category" field accordingly. Here's a step-by-step guide:

  1. In ServiceNow, navigate to "System Definition" > "Client Scripts."

  2. Click "New" to create a new client script.

  3. Select the "Type" as "onChange" and set the "Table" to "Knowledge" (kb_knowledge).

  4. In the "Applies to" section, select "This form only" or specify the form name if you have a custom form.

  5. In the "Fields" section, add the fields that should trigger the script: Knowledge Base (kb_knowledge_base), Category (kb_category), and Topic (your topic field).

  6. In the "Script" section, add the following code:

 

javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
var kbField = g_form.getReference('kb_knowledge_base', updateFields);
function updateFields(kbRecord) {
var kbName = kbRecord.name;
var categoryValue = g_form.getValue('kb_category');
var topicValue = g_form.getValue('<your_topic_field>'); // Replace <your_topic_field> with the actual field name
var secondCategoryField = '<your_second_category_field>'; // Replace <your_second_category_field> with the actual field name
if (kbName === 'HR General Knowledge' && categoryValue !== '' && topicValue === 'General') {
g_form.setMandatory(secondCategoryField, true);
g_form.clearOptions(secondCategoryField);
g_form.addOption(secondCategoryField, '', '-- Select an option --', 0);
for (var i = 1; i <= 5; i++) {
g_form.addOption(secondCategoryField, i.toString(), 'Choice ' + i);
}
} else {
g_form.setMandatory(secondCategoryField, false);
g_form.clearOptions(secondCategoryField);
}
}
}


 

  1. Replace <your_topic_field> and <your_second_category_field> with the actual field names in your instance.

Now, when the Knowledge Base is "HR General Knowledge," the "Category" field is not empty, and the "Topic" field is "General," the second "Category" field will become mandatory and display five choices.

Remember to adjust the field names and choice options to match your specific requirements.

View solution in original post

2 REPLIES 2

Daniel Biesiada
Mega Guru

 

To implement this requirement in ServiceNow, you can create a client script that listens for changes in the "Knowledge Base," "Category," and "Topic" fields, and sets the behavior of the second "Category" field accordingly. Here's a step-by-step guide:

  1. In ServiceNow, navigate to "System Definition" > "Client Scripts."

  2. Click "New" to create a new client script.

  3. Select the "Type" as "onChange" and set the "Table" to "Knowledge" (kb_knowledge).

  4. In the "Applies to" section, select "This form only" or specify the form name if you have a custom form.

  5. In the "Fields" section, add the fields that should trigger the script: Knowledge Base (kb_knowledge_base), Category (kb_category), and Topic (your topic field).

  6. In the "Script" section, add the following code:

 

javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
var kbField = g_form.getReference('kb_knowledge_base', updateFields);
function updateFields(kbRecord) {
var kbName = kbRecord.name;
var categoryValue = g_form.getValue('kb_category');
var topicValue = g_form.getValue('<your_topic_field>'); // Replace <your_topic_field> with the actual field name
var secondCategoryField = '<your_second_category_field>'; // Replace <your_second_category_field> with the actual field name
if (kbName === 'HR General Knowledge' && categoryValue !== '' && topicValue === 'General') {
g_form.setMandatory(secondCategoryField, true);
g_form.clearOptions(secondCategoryField);
g_form.addOption(secondCategoryField, '', '-- Select an option --', 0);
for (var i = 1; i <= 5; i++) {
g_form.addOption(secondCategoryField, i.toString(), 'Choice ' + i);
}
} else {
g_form.setMandatory(secondCategoryField, false);
g_form.clearOptions(secondCategoryField);
}
}
}


 

  1. Replace <your_topic_field> and <your_second_category_field> with the actual field names in your instance.

Now, when the Knowledge Base is "HR General Knowledge," the "Category" field is not empty, and the "Topic" field is "General," the second "Category" field will become mandatory and display five choices.

Remember to adjust the field names and choice options to match your specific requirements.

Hi,

It's working, thank you🙂.