- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hello,
We are exploring options to populate field values based on subcategory field on Incident form before submitting the ticket.
Templates will not work in our case.
Thank you.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hi @MThomas1 ,
I see the issue in your script. There are a couple of small mistakes that are causing it not to work:
1.Case sensitivity in g_form.setValue:
JavaScript is case-sensitive. In your first if block you wrote:
g_form.setvalue('description','This is a test');
It should be setValue with a capital V:
g_form.setValue('description','This is a test');
2.Other things to check:
Make sure your onChange client script is actually attached to the correct field (the one you’re monitoring).
Ensure the field values you are checking ('Testing' and 'Virus incident') exactly match the choice values (case-sensitive and no extra spaces).
Here’s the corrected script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
console.log('Form is loading or value is empty, exiting script.');
return;
}
console.log('New value detected: ' + newValue); // Logs new value
if (newValue == 'Testing') {
g_form.setValue('assignment_group', '87986d24db15a7c0de5b6572ca961939');
g_form.setValue('description','This is a test');
g_form.addInfoMessage('Assignment group and description set for Testing.');
console.log('Set values for Testing.');
}
else if (newValue == 'Virus incident') {
g_form.setValue('assignment_group', '75972d24db15a7c0de5b6572ca9619f1');
g_form.setValue('description', 'Hello World');
g_form.addInfoMessage('Assignment group and description set for Virus incident.');
console.log('Set values for Virus incident.');
}
else {
g_form.addInfoMessage('No matching value found.');
console.log('No matching value for: ' + newValue);
}
}
I have added some logs in the script.. which will help you debug the issue.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
It is working now.
Field values was causing the issue. I selected the the field label vs value.
Thank you very much for your support.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Glad to hear it’s working now! 😊
Thanks for the update and for confirming the solution. Happy to help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
You can achieve this using a combination of client scripts and reference qualifiers..... For instance, you can create an onChange client script that listens for changes to the Subcategory field and, based on its value, dynamically sets other fields like Assignment Group, Category, or Short Description. This approach allows you to implement conditional logic tailored to your specific requirements. Additionally, to ensure that the Subcategory field is populated appropriately, you can configure a reference qualifier on the Subcategory field that filters available options based on the selected Category, ensuring that only relevant subcategories are presented to the user.......
If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Thank you for your input.
Yes, Subcategory field value is dependent on Category field.
I will try client script and update the status soon.
Thank you.
