Auto-Complete on Catalog Reference Variables

Jim Coyne
Kilo Patron

The Auto-Complete feature with the AJAXTableCompleter is a nice feature in forms because you can add extra fields to display in the list to make it easier for users to tell similar items apart from one another (e.g. users with the same name).

Unfortunately, the feature is not available for Service Catalog variables - until now that is!!!

I wrote a UI Script that allows us to specify which columns to add to the list:


function u_enableAJAXTableCompleter(variableName, columns, orderBy) {
try{
var referenceFieldName = 'sys_display.' + g_form.getControl(variableName).name.toString();
var onfocusScript = $(referenceFieldName).readAttribute('onfocus').toString().replace('AJAXReferenceCompleter', 'AJAXTableCompleter');
if (Prototype.Browser.IE) {
var referenceField = document.getElementById(referenceFieldName);
var newOnFocusFunction = new Function(onfocusScript);
referenceField.ac_columns = columns;
referenceField.ac_order_by = orderBy;
referenceField.onfocus = newOnFocusFunction;
} else {
$(referenceFieldName).writeAttribute({ac_columns : columns, ac_order_by : orderBy, 'onfocus' : onfocusScript});
}
} catch(err) {}
}


The trick is to replace "AJAXReferenceCompleter" with "AJAXTableCompleter" and then add the columns. You just call it from an onLoad script with the name of the variable and the columns you want to add and the column to sort on:

function onLoad() {
u_enableAJAXTableCompleter('caller_id', 'email;phone;title;department', 'department');
}


Here's a screenshot of it running in a demo instance on the Create a new Incident record producer:

find_real_file.png

You can see the extra fields are being displayed and the data is sorted on the Department field.

Update: I still have not figured out how to allow the searching on the extra fields yet (ref_ac_columns_search=true). I've tried adding different combinations of "ref_ac_columns_search" and "ac_columns_search" as attributes without any luck.

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

So finally some good news, at least for Dublin users.   I just found out yesterday from a colleague of mine that it is now supported in Dublin.   It is a little misleading though.



Variables will inherit some of the attributes that may be defined as default attributes for the table (Auto-Complete for Reference Fields - ServiceNow Wiki), but searching on the extra columns will not work until you actually add the "ref_ac_columns" and "ref_ac_columns_search" attributes to the variable.



So, to add the ability to search the email address on a User reference variable when you have a default set on the User table (as per above wiki article), just add the "ref_ac_columns_search=true,ref_ac_columns=email" attributes to the variable.   If you do not have a default set, you will need the entire "ref_auto_completer=AJAXTableCompleter,ref_ac_columns=email,ref_ac_columns_search=true,ref_ac_order_by=name" attribute on the variable.


View solution in original post

21 REPLIES 21

MKhan1
Giga Contributor

Hi Jim,

I am in need to have search on extra columns. Any luck?.


I have not looked at this for a while now, but may try to find some time to look at it again. Not sure if Calgary fixes the issue?


Unfortunately the ref_ac_columns_search attribute still doesn't work in Calgary out of the box for catalog variables


geannaweed
Tera Contributor

Hi, it's 2014 and we are on dublin and i am wondering why the catalog variable watch list has no attribute? We are looking into showing the userID and email address column of the user table when someone finds someone in the watch list. Any ideas?


There's a note in the following wiki article - Auto-Complete for Reference Fields - ServiceNow Wiki



Note: Auto-complete cannot match text from additional columns when the reference field is a product of the ui_reference UI macro. This means any auto-complete against a selector, such as the Impersonate User list, can only match text against the display value.



So it would seem that list type fields still do not support it.