- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2023 11:08 PM - edited 03-24-2024 10:48 PM
Hi all, I'm creating a form that has a lookup select box (called department) as well as a requested for (user) reference field. The lookup select box lists certain departments (it has a reference qualifier to only show certain values), and has a "-- None --" option. I'm trying to make it so that when I change the "requested for" field, the lookup select box auto-populates with the user's department.
To do this, I'm using an onChange client script on the requested for field, making an AJAX call to the server, grabbing the user's department's sys_id, and then using that on the client side (via the userDeptSysId variable below) to update the lookup select box:
g_form.setValue("department", userDeptSysId); // userDeptSysId comes from a GlideAjax call
This works fine, however, the issue I'm facing is if userDeptSysId holds a value that isn't listed in the lookup select box dropdown, a new unwanted entry is created, whereas I would instead like "-- None --" to be selected:
I am fully aware that I can make my server script do a check to see if the user's department sys_id is in the department dropdown list by manually querying the department table myself and running the ref qualifier as an encoded query, but I'm wondering if there is an easier way - I'm more wondering if there is a simpler way (preferably on the client side), that allows me to check if the sys_id I get back is already a valid value within the lookup select box, and then select it if it is, otherwise, don't do anything (to avoid creating an unwanted option)?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2023 11:38 PM - edited 03-20-2023 11:41 PM
Yeah, I was hoping there was some sort of way to do something like:
g_form.isValidOption("look_up_select_field", userDeptSysId);
and then only set the value for the lookup select field if `userDeptSysId` is already a valid option.
I was able to come up with a little bit of a hack (this works because when a new option is added as an option it uses the sys_id as both the display value and value, which I can then detect and remove):
g_form.setValue("department", userDeptSysId);
clearInvalidOption("department");
function clearInvalidOption(field) {
var val = g_form.getValue(field);
var dispVal = g_form.getDisplayValue(field);
if(val === dispVal)
g_form.removeOption(field, val);
}
The main reason why I want to avoid using a server script here is because:
- It feels a bit messy to have the reference qualifier maintained in two places (one on the variable definition and one in the code to check if the department is one of the fileted ones). The reference qualifier is somewhat complex as it uses `javascript: ` to compute itself, so it's not something that could easily be stored in a system property for reuse.
- I have a generic script include that allows me to obtain user details, I'd rather not modify this if I don't have to (which I could do, but then I'd been making a separate ajax call, and also point 1 applies here as well).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2023 11:18 PM
if the lookup select box is filtered one then that sysId which you are setting should be having a corresponding record in that lookup select box
In which use-case the sysId won't be there?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2023 11:25 PM - edited 03-20-2023 11:27 PM
Hi @Ankur Bawiskar - thanks for the reply.
The actual case is a bit more complex, but the departments listed in the lookup select box are only those which have a certain parent. It is possible that the user selected in the requested for has a department that doesn't have that certain parent, and so their department won't have a corresponding value in the lookup select box. We still want users to be able to select the user in the "requested for" field even if they don't have a department that corresponds to the lookup select box, and instead the form would be submitted with the the department as "-- None --" (please ignore that department is mandatory in the above screenshot).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2023 11:28 PM
in that case you cannot check that in client side if sysId is present or not.
The only way is to check in server side if the department is one of the filtered ones, if not then set value with None.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2023 11:38 PM - edited 03-20-2023 11:41 PM
Yeah, I was hoping there was some sort of way to do something like:
g_form.isValidOption("look_up_select_field", userDeptSysId);
and then only set the value for the lookup select field if `userDeptSysId` is already a valid option.
I was able to come up with a little bit of a hack (this works because when a new option is added as an option it uses the sys_id as both the display value and value, which I can then detect and remove):
g_form.setValue("department", userDeptSysId);
clearInvalidOption("department");
function clearInvalidOption(field) {
var val = g_form.getValue(field);
var dispVal = g_form.getDisplayValue(field);
if(val === dispVal)
g_form.removeOption(field, val);
}
The main reason why I want to avoid using a server script here is because:
- It feels a bit messy to have the reference qualifier maintained in two places (one on the variable definition and one in the code to check if the department is one of the fileted ones). The reference qualifier is somewhat complex as it uses `javascript: ` to compute itself, so it's not something that could easily be stored in a system property for reuse.
- I have a generic script include that allows me to obtain user details, I'd rather not modify this if I don't have to (which I could do, but then I'd been making a separate ajax call, and also point 1 applies here as well).