- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2023 10:37 AM
I have a requirement on knowledge article form.
- 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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2023 10:42 AM
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:
In ServiceNow, navigate to "System Definition" > "Client Scripts."
Click "New" to create a new client script.
Select the "Type" as "onChange" and set the "Table" to "Knowledge" (kb_knowledge).
In the "Applies to" section, select "This form only" or specify the form name if you have a custom form.
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).
In the "Script" section, add the following code:
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);
}
}
}
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2023 10:42 AM
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:
In ServiceNow, navigate to "System Definition" > "Client Scripts."
Click "New" to create a new client script.
Select the "Type" as "onChange" and set the "Table" to "Knowledge" (kb_knowledge).
In the "Applies to" section, select "This form only" or specify the form name if you have a custom form.
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).
In the "Script" section, add the following code:
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);
}
}
}
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-18-2023 01:45 AM
Hi,
It's working, thank you🙂.