logo

NJP

Create a Reference Select2 field

Import · Feb 10, 2016 · article

ServiceNow uses a select field for reports that looks really cool:

image

A question came up on the community about how, exactly, you could create one. Digging into the elements of the field, we can find that it is a select2 field:

image

Select2 offers their code publicly: Select2 - The jQuery replacement for select boxes

So, this should not be too difficult a task to do. There are two paths you could take, downloading the scripts from select2 and adding them as global UI Scripts in your instance:

image

and calling them in UI stuff like:

or just calling them directly from the source:

Finally, your code would look something like:

<?xml version="1.0" encoding="utf-8" ?>  

 

   

   

   

    </p> <p>  $j(document).ready(function() {$j(&quot;.js-example-basic-single&quot;).select2();});   </p> <p>   

   

  var table = new GlideRecord('sys_db_object');  

  table.orderBy('label');  

  table.query();  

  /g:evaluate  

          ${table.label}     /j:while      

   

/j:jelly  

This just queries the tables of the system, like the report example, and adds all the results into options. The end result looks like this:

image

View original source

https://www.servicenow.com/community/in-other-news/create-a-reference-select2-field/ba-p/2280613