Issue with client Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2024 08:37 PM
How can we configure a client script to automatically populate the Impact and Urgency fields as '3 - Low' when the category is set to 'HRMS' and a related sub-category is selected in an incident?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2024 09:25 PM
Hi @pravallikag6589 ,
Follow below steps.
- Create 2 onChange client script, one for Category and 2nd for sub-category.
- Use below script for category client script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === oldValue) {
return;
}
// Check if the category is HRMS
if (newValue === 'HRMS') {
// Get the Subcategory value
var subcategory = g_form.getValue('subcategory');
// If subcategory is selected, set Impact and Urgency to '3 - Low'
if (subcategory) {
g_form.setValue('impact', '3 - Low');
g_form.setValue('urgency', '3 - Low');
}
}
}
3. For sub-category client script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === oldValue) {
return;
}
// Get the Category value
var category = g_form.getValue('category');
// If Category is HRMS and Subcategory is selected, set Impact and Urgency
if (category === 'HRMS') {
g_form.setValue('impact', '3 - Low');
g_form.setValue('urgency', '3 - Low');
}
}
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2024 09:31 PM
The script is triggered when a field changes .
It checks if the Category is set to HRMS .
It also checks if a Sub-Category is selected .
If both conditions are met, it uses g_form.setValue to set the Impact and Urgency fields to "3", which corresponds to Low.
(function() {
var cat = g_form.getValue('category');
var sub = g_form.getValue('sub_category');
if (cat== 'HRMS' && sub)
g_form.setValue('impact', '3');
g_form.setValue('urgency', '3');
}
})();
Make sure the field names in the script (category, sub_category, impact, and urgency) match the actual field names used in your instance. If the field names differ, you can find them in the dictionary for the Incident table.
If the values for Impact and Urgency are represented by labels (e.g., "3 - Low"),.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2024 09:38 PM
Hello @pravallikag6589
There are 2 corrections that you need to do.
For your future instances, please remember:
1. Condition should be set for the field that you're changing.
2. Always use backend values of the choicelist. You can select show choicelist to find backend values or can install snUtils chrome extension that will show backend values.
You can try below onChange Script:
Name: SH Set P4 HRMS
UI Type: All
Type: OnChange
Field Name: Subcategory
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var category = g_form.getValue('category');
// First check if the Category is HRMS
if (category == 'HRMS') {
if(newValue == 'systemLogin' || newValue == 'employeeInformation'){
g_form.setValue('impact', '3');
g_form.setValue('urgency', '3');
}
}
else{
g_form.setValue('impact', '');
g_form.setValue('urgency', '');
}
}
Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2024 10:05 PM
Hello @pravallikag6589
To meet the requirement, create an onChange client script
- Table: Incident
- Field: subcategory
- Type: onChange
Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Check the value of 'category' and 'subcategory'
var category = g_form.getValue('category');
var subcategory = g_form.getValue('subcategory');
// If the Category is 'HRMS' and a Subcategory is selected, set Impact and Urgency
if (category === 'hrms' && subcategory) {
g_form.setValue('impact', '3'); // '3 - Low'
g_form.setValue('urgency', '3'); // '3 - Low'
}
}This will get trigger when subcategory is selected and update to impact and urgency field only when category is 'hrms' and subcatory is not empty.
Client script configuration setup:
Hope this helps!
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar
