Dynamic filtered choice field within a service portal form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2023 11:07 AM
I'm creating a form within the service portal with which users can request different tasks to be done. The form fileds will change dynamically depending on the type of request the user is creating.
There are two fields that I will be using in order to do this :
- request_type (Form variable with string display value and a numerical value (1 to 5))
- request_task (Option field reflecting a custom table contaning task choices) This field is meant to be filtered according the request type.
My custom table contains two custom fields :
- u_request_type (integer field with choices ranging from 1 to 5)
- u_request_task (String field containing the choice description that will be display to represent the form field change)
Within my custom table all u_request_task are unique. u_request_type is used to create the one to many relationship.
I am using an onChange client script to try and filter request_task using the code below:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.clearOptions("request_task");
var request_type = g_form.getValue("request_type");
var request_task_list = new GlideRecord("u_task_choices");
request_task_list.addQuery("u_request_type", request_type);
request_task_list.query();
while (request_task_list.next()) {
g_form.addOptions("request_task", request_task_list.u_request_task, request_task_list.u_request_task);
}
}
The GlideRecord doesn't seem to return any value. Which prevents any options from being added. I've read somewhere that GlideRecord shouldn't be used within a client script, but all documentation I can find on the subject seems to be using it.
I must add that I am using a choice field instead of reference so I can manage the choices in a dropdown menu.
Thanks.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2023 11:33 AM
Hi,
In ServiceNow, using GlideRecord in a client script is generally not recommended because it can impact performance and may lead to potential security risks. Instead, you can use GlideAjax to fetch the data from the server asynchronously and update the form fields dynamically.
Here's an example of how you can modify your client script using GlideAjax to filter the "request_task" field based on the selected "request_type":
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
g_form.clearOptions('request_task');
var requestType = g_form.getValue('request_type');
var ga = new GlideAjax('FilterRequestTask');
ga.addParam('sysparm_name', 'getFilteredTasks');
ga.addParam('request_type', requestType);
ga.getXML(filterTasksCallback);
}
function filterTasksCallback(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
var tasks = JSON.parse(answer);
tasks.forEach(function(task) {
g_form.addOption('request_task', task.value, task.displayValue);
});
}
To set up the GlideAjax functionality:
- Create a new Script Include record:
- Name: FilterRequestTask
- Script: Use the following code:
var FilterRequestTask = Class.create();
FilterRequestTask.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getFilteredTasks: function() {
var requestType = this.getParameter('request_type');
var tasks = [];
var gr = new GlideRecord('u_task_choices');
gr.addQuery('u_request_type', requestType);
gr.query();
while (gr.next()) {
tasks.push({
value: gr.u_request_task,
displayValue: gr.u_request_task.getDisplayValue()
});
}
return JSON.stringify(tasks);
},
type: 'FilterRequestTask'
});
Thanks,
Rahul Kumar
Thanks,
Rahul Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2023 12:05 PM - edited 06-05-2023 12:43 PM
Hi,
Thank you for your assistance, I tried your proposed solution. During testing, my browser returned both these error messages:
- Failed to load resource: the server responded with a status of 404 (Not Found)
- Unhandled exception in GlideAjax.
I am not really familiar with GlideAjax, I notice in your script that you are using Value and displayValue, although It is for my custom table field. Would that be a problem?
Edit: it seems there was a typo in the script include's name. I'm only left with the unhandled exception error.