- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-04-2024 01:33 PM
Hello! I'm relatively new to ServiceNow and I'm having some trouble figuring this problem out. Basically, I want to use a query parameter that's passed in the URL to populate an existing select box/dropdown on a catalog item form. I've been able to achieve this with the following:
var params = getUrlParams();
var field= params['field_name'];
if (field) {
g_form.setValue('field_name', field);
g_form.setVisible('field_name', true);
}
According to the output from getControl, the select box has no options or values even though on the UI, the dropdown certainly has choices and I'm able to successfully submit the form. I've tried using g_form.addOption to add new options and see if the output from getControl changes but it doesn't (I still see that options is an array with length 0).
Please let me know if my understanding of any of these methods are wrong or if there is a better way to go about this. Thank you!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2024 01:13 PM
I don't know how you would be able to do this is front end if the options are not able to appear for you to compare with. So what if you tried to do the comparison on the backend? Use an AJAX call to get to the server side then do a glide record looking in the Question Choice Table <question_choice>. Pass the "question" as the search key for the Glide Record Query along with the url parm value to compare against. The only issue that I can think of right now is that you might have issue if the same Question is asked in a different form - you would need to find some way to ID the right question at a sys_id level or in combination with the item name.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2024 01:13 PM
I don't know how you would be able to do this is front end if the options are not able to appear for you to compare with. So what if you tried to do the comparison on the backend? Use an AJAX call to get to the server side then do a glide record looking in the Question Choice Table <question_choice>. Pass the "question" as the search key for the Glide Record Query along with the url parm value to compare against. The only issue that I can think of right now is that you might have issue if the same Question is asked in a different form - you would need to find some way to ID the right question at a sys_id level or in combination with the item name.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2024 03:59 PM
Hi there,
This should work:
var params = getUrlParams();
var field = params['field_name'];
if (field) {
// Get the form element (HTML) for the select box
var formElement = g_form.getControl('field_name');
var isValid = false;
// Loop through the available options within the select element
for (var i = 0; i < formElement.options.length; i++) {
if (formElement.options[i].value === field) {
isValid = true;
break;
}
}
// Only set the value if it's valid
if (isValid) {
g_form.setValue('field_name', field);
g_form.setVisible('field_name', true);
} else {
console.log("Invalid choice passed in the URL parameter");
}
}