Display referenced records after 3 letters client script doesn't work
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2024 10:38 PM
Hi,
I have a client request to display referenced records on a reference field but only after a user types 3 letters. Can this be achieved?
When I try to make this issue work with client script the value is being registered as chosen only after I pick a value from the referenced records. This is why I am stuck, Can this somehow be achieved in a different way maybe?
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2024 11:25 PM
I've never seen anything that alters the type-ahead search functionality like that.
I wonder what the payoff is for all the customization you'd have to do to pull it off.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2024 03:49 AM
Me neither and I am curious to learn if this is achievable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2024 01:16 AM
Hello @MikeMU ,
Yes, it is possible to achieve this requirement using client-side scripting in ServiceNow. You can use an onChange client script on the reference field to trigger a search for referenced records only after the user has typed at least three letters. Here's how you can implement it:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Check if the length of the entered value is at least three letters
if (newValue.length >= 3) {
// Perform a GlideAjax call or use GlideRecord queries to search for referenced records
// and populate the reference field accordingly
// Example:
// performSearch(newValue);
gs.addInfoMessage("Searching for referenced records after typing at least three letters.");
} else {
// If the length of the entered value is less than three letters,
// clear the reference field or display a message to the user
// Example:
// g_form.clearValue('your_reference_field');
gs.addInfoMessage("Please type at least three letters to search for referenced records.");
}
}
I hope it works for you
Please mark my answer Correct/Helpful, If applicable
Regards,
Sujit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2024 03:48 AM
Hey Sujit,
as I wrote. The client script is not being triggered unless I already choose the value.