treycarroll
Giga Guru

We have a team of developers building catalog items.   As I review the code that they are writing, I'm finding that much of it is extremely repetitive.     We've built out a library of Script includes to support common operations, but I was brainstorming some ways to accelerate development with some "syntactic sugar".   Granted, it's only a couple of lines difference when you consider that we already have script includes to do these things, but I personally find that little stuff like this "breaks my flow".

This idea was not universally well-received on our dev team, so I'm looking for some feedback from the community.   sabell2012 I would appreciate your feedback.

Here they are:

1. The currentUser property gives you instant access to any property on the current user.

GlideRecord.prototype.currentUser = (function () {

      var gr = new GlideRecord('sys_user');

      if (gr.get(new GlideSystem().getUserID())) {

              return gr;

      }

})();

var gr = new GlideRecord('sc_task');

gr.addQuery('assigned_to.email', gr.currentUser.email); //Yes, this is contrived.   We could have just used the sys_id

gr.query();

while (gr.next()) {

      //Do something awesome here

}

2. The getUser method gives you quick access to properties on any user where you have the sys_id, the user_name or the email.

GlideRecord.prototype.getUser = function () {

      var gr = new GlideRecord('sys_user');

      gr.addEncodedQuery('sys_id=' + arguments[0] + '^ORuser_name=' + arguments[0] + '^ORemail=' + arguments[0]);

      gr.query();

      if (gr.next()) {

              return gr;

      }

};

//Assuming that we're in a workflow on a catalog item

var gr = new GlideRecord('incident');

gr.addQuery('assignment_group.manager.email', gr.getUser(current.variables.requested_for).email);//This is contrived.   Email is not necessary

gr.query();

while (gr.next()) {

      //Do something awesome here

}

// From an INC Business Rule

var gr = new GlideRecord('sys_email');

gr.addQuery('recipients', 'CONTAINS', gr.getUser(current.assigned_to).email);

gr.addQuery('subject', 'CONTAINS', current.number);

gr.query();

while (gr.next()) {

      //Do something awesome here

}

3. currentUserGroups gives quick access to a list of the current user's groups.urrent

UserGrcuoup

GlideRecord.prototype.currentUserGroups = (function () {

      var grps = {};//object keys are unique so we use it to avoid duplication of ids

      var gr = new GlideRecord('sys_user_grmember');

      gr.addQuery('user', new GlideSystem().getUserID());

      gr.query();

      while (gr.next()) {

              grps[gr.group.sys_id.toString()] = true;

      }

      var ret = '';

      for (var k in grps) {

              ret += k + ',';

      }

      ret = ret.slice(0, -1);//remove the final comma

      return ret;

})();

var g = new GlideRecord('sys_user_grmember'); //Another highly contrived example.   It shows the group names for the current user.
g.addQuery('user', g.currentUser.sys_id);
g.addQuery('group', 'IN', g.currentUserGroups);
g.query();

while (g.next()) {
    gs.print(g.group.name);
}

var gr = new GlideRecord('incident');

gr.addQuery('assignment_group','IN', gr.currentUserGroups);

gr.query();

while (gr.next()) {

      //Do something awesome here

}

Opinions/concerns that were raised:

1) An extension method is not justifiable unless it addresses a problem; syntactic sugar is not a good reason for a creating an extension method.

2) SN may remove our ability to inject things via prototype in the future, which would cause significant rework on all code written using these methods and properties.

2 Comments