How to select a value from a lookup select box only if it exists

Nick Parsons
Mega Sage

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:

NickParsons_0-1679378372691.png

 

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)?

1 ACCEPTED SOLUTION

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:

  1. 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. 
  2. 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).

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@Nick Parsons 

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?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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).

@Nick Parsons 

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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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:

  1. 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. 
  2. 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).