logo

NJP

Reference field autocomplete - sort the list in descending order

Import · Nov 21, 2021 · article

There are several questions in community that might be related to the same functionality, but since neither of them completely repeats my request, I decided to write a separate article, referencing these questions below.

Introduction

There is autocomplete in Reference fields to provide a user more convenient way to select the relevant record from the list, without additional clicks on the magnifying glass. However, some functionality that seems trivial is either not available, or can be reached only through a workaround.

Sort in descending order

There is the attribute in Reference fields called "ref_ac_order_by". It works great, but the default sorting order is ascending (a-z) and there is no option to change it. If one sorts by a String field, the sorting order a-z perfectly makes sense. But if the sorting should go by a date or a record number, it is usually would make more sense to see the newer on top, and currently it's not.

image

Is there a way to sort the list in descending order?

Answer: yes, there is. Combination of "ref_ac_order_by" dictionary attribute and before query business rule is required.

  1. Add the "ref_ac_order_by=" to the reference field.
  2. Create the before query business rule as below:
    (function executeRule(current, previous /*null when async*/) {
    current.orderByDesc('');
    })(current, previous);

NOTE: According to my check, it works only in the combination, at least in my specific case.

Increase the number of displayed rows

The default in the autocomplete list is 15 records.

image

There is a system property to control it. However, what if there is no need to change the default in the whole instance but only for a specific field. Is it possible?

Answer: no, it's not. The official answer of the ServiceNow support:

"There is no other way to limit it. It can be only be done for a sys_property affecting the complete instance.

It cannot be field specific."

NOTE: Both questions are NOT RELATED to the pop-up list that appears once the "Lookup using list" (magnifying glass) is clicked, only to the autocomplete list that appears once user starts typing something in the reference field.

Related links

List of all available dictionary attributes (ServiceNow documentation)

Auto-complete for reference fields (ServiceNow documentation)

Sorting in descending order for reference type variables in servicenow portal (Community question)

Sorting list order is not working in List collector (Community question)

How can I get a reference field's pick list to display in a sorted order? (Community question)

View original source

https://www.servicenow.com/community/process-mining-article/reference-field-autocomplete-sort-the-list-in-descending-order/ta-p/2309400