Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Chuck Tomasi
Tera Patron

A colleague of mine recently asked for some assistance in managing Glide List (glide_list) fields. I had written some scripts that were fairly task specific so I rewrote them as generic as possible and share them here.

Glide List fields are a field that can hold many reference values at a time (as opposed a reference field that holds just one value.) The most familiar example of a glide list is the Watch list field on the task table (incident, problem, change, etc.) However, this script works on any glide_list.


find_real_file.png

Behind the scenes, the system is storing these values as comma separate sys_ids. Occasionally, it becomes necessary to manage the data in that field. For example, one might want to:

  • Know if a user exists in the list
  • Add a new user to the list
  • Delete a user from the list 

For that, I created the following script include I call ListUtil. This is based on an example given at Knowledge 11. I added a _debug function and a generous amount of comments to describe each function.

To use the ListUtil class, create a new object and pass the field (values) you want to manage:

var lu = new ListUtil(current.watch_list);


Here is a summary of the functions (methods) available:

  • isInList(id) - returns true/false if id is in the list
  • addToList(id) - adds id to the list if it doesn't already exist
  • removeFromList(id) - removes id from the list
  • getList() - returns the comma separated string of the current list (e.g. to save back to current.watch_list) 
var ListUtil = Class.create();
ListUtil.prototype = {

      initialize: function(list) {

              this.debug = gs.getProperty('debug.ListUtil') == 'true';

              if (JSUtil.notNil(list)) {
                      this._list = list.split(',');
              } else {
                      this._list = [];
              }
      },

/*
* isInList - check if an id is in the list
*
* @param id - sys_id to look for
* @return - boolean
* @uses - this._list
*
*/
      isInList : function(id) {

              var i = this._getIndex(id);

              this._debug('isInList(' + i + '): looking for ' + id + ' in ' + this.getList());
              return (i > -1);
      },

/*
* addToList - add an id to the list
*
* @param id - sys_id of the id to add
* @return - NA
* @uses - this._list
*
*/
      addToList : function(id) {

              if (!this.isInList(id))
                      this._list.push(id);
      },
/*
* removeFromList - remove a id from the list
*
* @param id - sys_id to remove
* @return - NA
* @uses - this._list
*
*/
      removeFromList : function(id) {

              var index = this._getIndex(id);

              if (index > -1)
                      this._list.splice(index, 1);
      },

/*
* getList - display the list as CSV
*
* @param - NA
* @return - string of CSV
* @uses - this._list
*
*/
      getList : function() {

              return this._list.join(',');
      },

/*
* _getIndex - return the index of an element in list
*
* @param id - id to look for
* @return - position of id in list (0..n/-1 = not found)
* @uses - this._list
*
*/
      _getIndex : function(id) {

              for (var i = 0; i < this._list.length; i++) {

                      if (this._list[i] == id)
                              return i;
              }

              return -1;
      },

/*
* _debug - print a message to the debug log
*
* @param s - string to print
* @return - NA
* @usrs - this._debug
*
*/
      _debug : function(s) {

              if (this.debug)
                      gs.print('>>>DEBUG: ListUtil: ' + s);
      },

      type: 'ListUtil'
}

Here's an sample script that demonstrates the various functions:

var inc = '04b28ae2c0a8016600c8f3d86e3278f4'; // INC0000066
var usr1 = '46d44a23a9fe19810012d100cca80666'; // Beth Anglin
var usr2 = '5137153cc611227c000bbd1bd8cd2005'; // Fred Luddy
var usr3 = '62826bf03710200044e0bfc8bcbe5df1'; // Abel Tuter
var gr = new GlideRecord('incident');

if (!gr.get(inc)) {
      gs.print('Error reading record.');

} else {
      var lu = new ListUtil(gr.watch_list);

      gs.print('Fred is in the list: ' + lu.isInList(usr2));

      lu.removeFromList(usr2); // Take Fred out of the list
      gs.print('Removed Fred: list=' + lu.getList());

      lu.addToList(usr3); // Add Abel to the list
      gs.print('Added Abel: list=' + lu.getList());

      lu.addToList(usr2); // add Fred back in
      lu.addToList(usr3); // add Abel (again)
      gs.print('Final: list=' + lu.getList());

      // Save the current list to the record and update
      gr.watch_list = lu.getList();
      gr.update();
}
39 Comments