how to trigger autofill from a client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2015 12:50 PM
Hello all. I have a field that I am populating from a client script. That field is a reference auto complete field. If I manually type in a value, the auto complete properly works and I get a list of matching entries below the field. When I populate it from a client script via the DOM (document.getelementById('fieldname').value = ), the value shows up in the field, but the auto complete doesn't trigger.
I'm not able to lookup the sys_id and store that instead, because there will likely be more than one match based on the value being entered. It's not possible for me to determine which one to pick programmatically and enter the sys_id.
How can I get the auto complete drop down to trigger when entering the value from a client script?
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2015 12:53 PM
Hi Charles,
It is important to avoid using DOM manipulation to take action via Client Scripts. The best bet is to use g_form.setValue to set any field.
GlideForm (g form) - ServiceNow Wiki
EDIT: Additionally, in terms of being unable to identify a unique sys_id programmatically, this is precisely why Autocomplete puts the control in the hands of the user. The user selects a uniquely identified record. So you may need to rethink your approach to allow for unique identification programmatically.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2015 06:42 PM
Thanks. I'm well aware of the pitfalls of using DOM manipulation. g_form.SetValue doesn't work for me unless I set the sys_id.
The business requirements behind this require that the autocomplete trigger. I was really hoping I could do that programmatically.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2015 07:28 PM
Hey Charles,
I think I see where you are trying to go with this. Best practices aside for a moment (translation: this will be an ugly hack), lets see if we can address this requirement:
var elem = $('sys_display.<table_name>.<field_name>');
elem.focus();
elem.value = 'YOUR VALUE HERE';
I ran some quick experiments and this appears to work. The theory at work here is that we can artificially trigger the AJAXAutoCompleter by simulating user interaction programmatically. This means interacting with the display input box. Line 2 sets the focus on the input which simulates focus from the user clicking the box (which if you watch the console triggers the AJAXAutoCompleter). Line 3 simulates entering the text. This seems to trigger the autocomplete dropdown though you will want to test thoroughly and make sure timing won't be an issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2015 06:05 AM
Hi Travis, what browser and version of ServiceNow are you testing with? What you have above I already tried, except with .focus() after setting the .value property, and it didn't "drop down". It did a direct match only after the field loses focus. At no point do I see a drop down as if I typed it.
I'm on glide-dublin-09-13-2013__patch7-hotfix2-12-12-2014. I tried FireFox, IE11, and Chrome, and they all behave the same way.