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

Jace Benson
Mega Sage

Very cool!


nikita_mironov
Kilo Guru

I wonder how to configure the Auto-Complete in Reference Variables to Match Text From Any Reference Field displayed using ac_columns.

I tried amending the UI Script as follows:

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.ac_columns_search=true;
referenceField.onfocus = newOnFocusFunction;
} else {
$(referenceFieldName).writeAttribute({ac_columns : columns, ac_order_by : orderBy, ac_columns_search : true, 'onfocus' : onfocusScript});
}
} catch(err) {}
}

And it displays additional columns but does not search over additional columns. Any hint?


Still no luck with the "ref_ac_columns_search" option.


WESAdmin
Kilo Contributor

Hi Jim,
I'm struggling to get this script to work in my instance, can you point me to the demo instance you have this script working on please?